Friday puzzle

My good friend Frank just sent me this email (I did translate to English though). I had never seen the algorithm before, so it gave me a good laugh. Now I need to share it. It’s written in Groovy:

If you just need to get some blood through the neural cells after the pork roast, try to figure out the code below.

Hint: a classic sort algorithm, but which one?

—————————————–

import java.util.concurrent.*

def list = new CopyOnWriteArrayList()

def task = { n ->
t = {
sleep(n);
list.add(n)
} // job in a closure
t as Callable // return block as a callable }

def workers = []

[5, 3, 7, 2, 1, 1, 3, 4].each { n ->
workers.add(task(n))
}

def executor = Executors.newFixedThreadPool(workers.size());
def futures = executor.invokeAll(workers, 10, TimeUnit.SECONDS);

println list # the sorted list


So, what is it? Eh’? Eh’? :o)

Advertisements

Please leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s