<?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>GPars &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/gpars/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:28:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>GPars &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Groovy + GPars: Handling Concurrency</title>
		<link>https://myshittycode.com/2017/04/06/groovy-gpars-handling-concurrency/</link>
					<comments>https://myshittycode.com/2017/04/06/groovy-gpars-handling-concurrency/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 07 Apr 2017 02:37:44 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[GPars]]></category>
		<category><![CDATA[Guava]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=939</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume given a list of user IDs (ex: 1, 2, 3, 4, and 5), we need to query 2 data sources to get the names and the addresses before returning a list of Employee objects. The Employee class looks like this:- We are going to explore multiple solutions to implement lookup(..) to run [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/06/groovy-gpars-handling-concurrency/">Groovy + GPars: Handling Concurrency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume given a list of user IDs (ex: 1, 2, 3, 4, and 5), we need to query 2 data sources to get the names and the addresses before returning a list of <code>Employee</code> objects. The <code>Employee</code> class looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
class App {
    String nameLookup(Integer id) {
        log(&quot;Name lookup: ${id}&quot;)
        Thread.sleep(2000)
        &quot;User ${id}&quot;
    }

    String addressLookup(Integer id) {
        log(&quot;Addr lookup: ${id}&quot;)
        Thread.sleep(4000)
        &quot;Address ${id}&quot;
    }

    void log(String message) {
        println &quot;${Thread.currentThread()} - ${new Date().format(&#039;HH:mm:ss&#039;)} - ${message}&quot;
    }

    List&amp;lt;Employee&gt; lookup(List&amp;lt;Integer&gt; userIds) {
        ???
    }

    void run() {
        def start = System.currentTimeMillis()

        def employees = lookup(&#x5B;1, 2, 3, 4, 5])

        def end = System.currentTimeMillis()

        println &#039;---------------------------------&#039;
        println &quot;Total time in seconds: ${(end - start) / 1000}&quot;
        println &#039;---------------------------------&#039;

        employees.each {
            println it
        }
    }

    static void main(String&#x5B;] args) {
        new App().run()
    }
}
</pre></div>


<p>We are going to explore multiple solutions to implement <code>lookup(..)</code> to run as fast as possible.</p>



<h2 class="wp-block-heading">ATTEMPT 1</h2>



<p>The simplest and the most straightforward approach is to perform the task synchronously.</p>



<p>Code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
List&lt;Employee&gt; lookup(List&lt;Integer&gt; userIds) {
    userIds.collect { id -&gt;
        new Employee(
                id: id,
                name: nameLookup(id),
                address: addressLookup(id)
        )
    }
}
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Thread&#x5B;main,5,main] - 20:54:09 - Name lookup: 1
Thread&#x5B;main,5,main] - 20:54:11 - Addr lookup: 1
Thread&#x5B;main,5,main] - 20:54:15 - Name lookup: 2
Thread&#x5B;main,5,main] - 20:54:17 - Addr lookup: 2
Thread&#x5B;main,5,main] - 20:54:21 - Name lookup: 3
Thread&#x5B;main,5,main] - 20:54:23 - Addr lookup: 3
Thread&#x5B;main,5,main] - 20:54:27 - Name lookup: 4
Thread&#x5B;main,5,main] - 20:54:29 - Addr lookup: 4
Thread&#x5B;main,5,main] - 20:54:33 - Name lookup: 5
Thread&#x5B;main,5,main] - 20:54:35 - Addr lookup: 5
---------------------------------
Total time in seconds: 30.129
---------------------------------
Employee(1, User 1, Address 1)
Employee(2, User 2, Address 2)
Employee(3, User 3, Address 3)
Employee(4, User 4, Address 4)
Employee(5, User 5, Address 5)
</pre></div>


<p>Since everything runs synchronously in one thread, it takes about 30 seconds to complete.</p>



<h2 class="wp-block-heading">ATTEMPT 2</h2>



<p>In this attempt and the rest of the attempts, we are going to use GPars, which is a concurrency and parallelism library.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupId&gt;org.codehaus.gpars&lt;/groupId&gt;
    &lt;artifactId&gt;gpars&lt;/artifactId&gt;
    &lt;version&gt;1.2.1&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p>In this attempt, we are going to use <code>GParsPool.executeAsyncAndWait(..)</code>.</p>



<p>Code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
List&lt;Employee&gt; lookup(List&lt;Integer&gt; userIds) {
    (List&lt;Employee&gt;) GParsPool.withPool {
        userIds.collect { id -&gt;
            def results = GParsPool.executeAsyncAndWait(
                    this.&amp;nameLookup.curry(id),
                    this.&amp;addressLookup.curry(id)
            )

            new Employee(
                    id: id,
                    name: results&#x5B;0],
                    address: results&#x5B;1]
            )
        }
    }
}
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Thread&#x5B;ForkJoinPool-1-worker-2,5,main] - 20:54:40 - Addr lookup: 1
Thread&#x5B;ForkJoinPool-1-worker-1,5,main] - 20:54:40 - Name lookup: 1
Thread&#x5B;ForkJoinPool-1-worker-2,5,main] - 20:54:44 - Name lookup: 2
Thread&#x5B;ForkJoinPool-1-worker-1,5,main] - 20:54:44 - Addr lookup: 2
Thread&#x5B;ForkJoinPool-1-worker-1,5,main] - 20:54:48 - Name lookup: 3
Thread&#x5B;ForkJoinPool-1-worker-2,5,main] - 20:54:48 - Addr lookup: 3
Thread&#x5B;ForkJoinPool-1-worker-1,5,main] - 20:54:52 - Addr lookup: 4
Thread&#x5B;ForkJoinPool-1-worker-2,5,main] - 20:54:52 - Name lookup: 4
Thread&#x5B;ForkJoinPool-1-worker-2,5,main] - 20:54:56 - Addr lookup: 5
Thread&#x5B;ForkJoinPool-1-worker-1,5,main] - 20:54:56 - Name lookup: 5
---------------------------------
Total time in seconds: 20.184
---------------------------------
Employee(1, User 1, Address 1)
Employee(2, User 2, Address 2)
Employee(3, User 3, Address 3)
Employee(4, User 4, Address 4)
Employee(5, User 5, Address 5)
</pre></div>


<p>This allows us to run both the name lookup and the address lookup concurrently for each user ID.</p>



<p><b>Speed gain compared to first attempt: 1.5x</b></p>



<h2 class="wp-block-heading">ATTEMPT 3</h2>



<p>How about <code>collectParallel(..)</code>?</p>



<p>Code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
List&lt;Employee&gt; lookup(List&lt;Integer&gt; userIds) {
    (List&lt;Employee&gt;) GParsPool.withPool {
        userIds.collectParallel { Integer id -&gt;
            new Employee(
                    id: id,
                    name: nameLookup(id),
                    address: addressLookup(id)
            )
        }
    }
}
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Thread&#x5B;ForkJoinPool-2-worker-1,5,main] - 20:55:00 - Name lookup: 1
Thread&#x5B;ForkJoinPool-2-worker-4,5,main] - 20:55:00 - Name lookup: 4
Thread&#x5B;ForkJoinPool-2-worker-3,5,main] - 20:55:00 - Name lookup: 2
Thread&#x5B;ForkJoinPool-2-worker-2,5,main] - 20:55:00 - Name lookup: 3
Thread&#x5B;ForkJoinPool-2-worker-5,5,main] - 20:55:00 - Name lookup: 5
Thread&#x5B;ForkJoinPool-2-worker-4,5,main] - 20:55:02 - Addr lookup: 4
Thread&#x5B;ForkJoinPool-2-worker-3,5,main] - 20:55:02 - Addr lookup: 2
Thread&#x5B;ForkJoinPool-2-worker-1,5,main] - 20:55:02 - Addr lookup: 1
Thread&#x5B;ForkJoinPool-2-worker-2,5,main] - 20:55:02 - Addr lookup: 3
Thread&#x5B;ForkJoinPool-2-worker-5,5,main] - 20:55:02 - Addr lookup: 5
---------------------------------
Total time in seconds: 6.156
---------------------------------
Employee(1, User 1, Address 1)
Employee(2, User 2, Address 2)
Employee(3, User 3, Address 3)
Employee(4, User 4, Address 4)
Employee(5, User 5, Address 5)
</pre></div>


<p>This allows us to run all name lookups concurrently, followed by all address lookup concurrently.</p>



<p><b>Speed gain compared to first attempt: 4.9x</b></p>



<h2 class="wp-block-heading">ATTEMPT 4</h2>



<p>Perhaps, another approach is to kick start all the lookups asynchronously and hold on to the returned <code>Future</code> objects:-</p>



<p>Code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
List&lt;Employee&gt; lookup(List&lt;Integer&gt; userIds) {
    (List&lt;Employee&gt;) GParsPool.withPool {
        List&lt;Future&gt; nameFutures = userIds.collect {
            this.&amp;nameLookup.callAsync(it)
        }

        List&lt;Future&gt; addressFutures = userIds.collect {
            this.&amp;addressLookup.callAsync(it)
        }

        userIds.withIndex().collect { Integer id, Integer i -&gt;
            new Employee(
                    id: id,
                    name: nameFutures&#x5B;i].get(),
                    address: addressFutures&#x5B;i].get()
            )
        }
    }
}
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Thread&#x5B;ForkJoinPool-3-worker-5,5,main] - 20:55:06 - Name lookup: 5
Thread&#x5B;ForkJoinPool-3-worker-4,5,main] - 20:55:06 - Name lookup: 4
Thread&#x5B;ForkJoinPool-3-worker-2,5,main] - 20:55:06 - Name lookup: 2
Thread&#x5B;ForkJoinPool-3-worker-3,5,main] - 20:55:06 - Name lookup: 3
Thread&#x5B;ForkJoinPool-3-worker-1,5,main] - 20:55:06 - Name lookup: 1
Thread&#x5B;ForkJoinPool-3-worker-3,5,main] - 20:55:08 - Addr lookup: 4
Thread&#x5B;ForkJoinPool-3-worker-2,5,main] - 20:55:08 - Addr lookup: 3
Thread&#x5B;ForkJoinPool-3-worker-4,5,main] - 20:55:08 - Addr lookup: 2
Thread&#x5B;ForkJoinPool-3-worker-5,5,main] - 20:55:08 - Addr lookup: 1
Thread&#x5B;ForkJoinPool-3-worker-1,5,main] - 20:55:08 - Addr lookup: 5
---------------------------------
Total time in seconds: 6.032
---------------------------------
Employee(1, User 1, Address 1)
Employee(2, User 2, Address 2)
Employee(3, User 3, Address 3)
Employee(4, User 4, Address 4)
Employee(5, User 5, Address 5)
</pre></div>


<p>It&#8217;s slightly better than the previous attempt, but the performance gain is rather negligible.</p>



<p><b>Speed gain compared to first attempt: 5.0x</b></p>



<h2 class="wp-block-heading">ATTEMPT 5</h2>



<p>What if we take the previous attempt and increase the number of threads to 10?</p>



<p>Code:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [2]; title: ; notranslate">
List&lt;Employee&gt; lookup(List&lt;Integer&gt; userIds) {
    GParsPool.withPool 10, {
        List&lt;Future&gt; nameFutures = userIds.collect {
            this.&amp;nameLookup.callAsync(it)
        }

        List&lt;Future&gt; addressFutures = userIds.collect {
            this.&amp;addressLookup.callAsync(it)
        }

        userIds.withIndex().collect { Integer id, Integer i -&gt;
            new Employee(
                    id: id,
                    name: nameFutures&#x5B;i].get(),
                    address: addressFutures&#x5B;i].get()
            )
        }
    }
}
</pre></div>


<p>Output:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Thread&#x5B;ForkJoinPool-4-worker-1,5,main] - 20:55:12 - Name lookup: 1
Thread&#x5B;ForkJoinPool-4-worker-2,5,main] - 20:55:12 - Name lookup: 2
Thread&#x5B;ForkJoinPool-4-worker-3,5,main] - 20:55:12 - Name lookup: 3
Thread&#x5B;ForkJoinPool-4-worker-4,5,main] - 20:55:12 - Name lookup: 4
Thread&#x5B;ForkJoinPool-4-worker-5,5,main] - 20:55:12 - Name lookup: 5
Thread&#x5B;ForkJoinPool-4-worker-6,5,main] - 20:55:12 - Addr lookup: 1
Thread&#x5B;ForkJoinPool-4-worker-8,5,main] - 20:55:12 - Addr lookup: 3
Thread&#x5B;ForkJoinPool-4-worker-7,5,main] - 20:55:12 - Addr lookup: 2
Thread&#x5B;ForkJoinPool-4-worker-9,5,main] - 20:55:12 - Addr lookup: 4
Thread&#x5B;ForkJoinPool-4-worker-10,5,main] - 20:55:12 - Addr lookup: 5
---------------------------------
Total time in seconds: 4.016
---------------------------------
Employee(1, User 1, Address 1)
Employee(2, User 2, Address 2)
Employee(3, User 3, Address 3)
Employee(4, User 4, Address 4)
Employee(5, User 5, Address 5)
</pre></div>


<p><b>Speed gain compared to first attempt: 7.5x</b></p>



<p>And now, we have successfully improve our implementation performance from 30 seconds to 4 seconds.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/06/groovy-gpars-handling-concurrency/">Groovy + GPars: Handling Concurrency</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/04/06/groovy-gpars-handling-concurrency/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">939</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk (Request-wide modification query)

Served from: myshittycode.com @ 2026-02-19 22:35:19 by W3 Total Cache
-->