<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shlrm.org Blag &#187; threads</title>
	<atom:link href="http://shlrm.org/wordpress/tag/threads/feed/" rel="self" type="application/rss+xml" />
	<link>http://shlrm.org/wordpress</link>
	<description>Linux, Java, Ruby, and Politics.</description>
	<lastBuildDate>Tue, 07 Sep 2010 13:38:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Groovy Threading and Java Threading</title>
		<link>http://shlrm.org/wordpress/2009/11/30/groovy-threading-and-java-threading/</link>
		<comments>http://shlrm.org/wordpress/2009/11/30/groovy-threading-and-java-threading/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 05:25:59 +0000</pubDate>
		<dc:creator>David Kowis</dc:creator>
				<category><![CDATA[Coding!]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://shlrm.org/wordpress/?p=307</guid>
		<description><![CDATA[I must be doing something wrong in groovy. Of course I'm a groovy noob, so that is probably the case.]]></description>
			<content:encoded><![CDATA[<p>I must be doing something wrong regarding my groovy code. I was talking to a friend and fiddling with a tower of hanoi solver code. Just burns in CPU basically. Now I hacked it up in groovy, after he talked about it, just because. Well I came up with a groovy-ish solution that should do the same thing his java code did. However, the Java code actually ran like 8 threads, whereas my groovy code only ran about 3. Based entirely on CPU usage in linux. On the same box. I don&#8217;t understand&#8230; The code follows.<span id="more-307"></span></p>
<p>Groovy Code:</p>
<pre class="brush:groovy">import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.Executors
import java.util.concurrent.Callable

def moveSingleDisk(source, dest) {
	//nothing!
}

def moveTower(number, source, dest, temp) {
	if (number &gt; 0) {
		moveTower(number -1, source, temp, dest)
		moveSingleDisk(source, dest)
		moveTower(number - 1, temp, dest, source)
	}
}

AtomicInteger counter = new AtomicInteger()
counter.set(1)

synchronized out(message) {
	println message
}

def logic = {
	start = System.nanoTime()
	moveTower(it, 'A','B','C')
	stop = System.nanoTime()
	out("${it} took ${(stop-start)/ 10**6} milliseconds")
}

def THREADS=16

pool = Executors.newFixedThreadPool(THREADS)
println "Creating a pool for $THREADS threads"

1.upto(64) {
	def value = it
	//out "Submitting job $value"
	task = {c -&gt; pool.submit( c as Callable)}
	task{logic(value)}
}

pool.shutdown()</pre>
<p>Java code:</p>
<pre class="brush:java">/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hanoi2;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author brad
 */
public class Main {

	static private ExecutorService exec = Executors.newFixedThreadPool(8);

	static void moveSingleDisk(char src, char dst) {
		// System.out.println(src + " =&gt; " + dst);
	}

	static void moveTower(int n, char src, char dst, char tmp) {
		if (n &gt; 0) {
			moveTower(n - 1, src, tmp, dst);
			moveSingleDisk(src, dst);
			moveTower(n - 1, tmp, dst, src);
		}
	}

	public void run() {
	}

	public static void main(String[] args) {

		System.out.println("Starting with a thread pool of 8");

		for (int i = 0; i &lt; = 64; i++) {
//      long start, stop;
			final int blah = i;
			exec.submit(new Runnable() {
				public void run() {
					long start = System.nanoTime();
					moveTower(blah, 'A', 'B', 'C');
					long stop = System.nanoTime();
					System.out.println(blah + " took " + ((stop - start) / 1000000) + "ms");

				}
			});
		}
	}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://shlrm.org/wordpress/2009/11/30/groovy-threading-and-java-threading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
