<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Sample usage</title><link>https://sourceforge.net/p/jparallelloops/wiki/Sample%2520usage/</link><description>Recent changes to Sample usage</description><atom:link href="https://sourceforge.net/p/jparallelloops/wiki/Sample%20usage/feed" rel="self"/><language>en</language><lastBuildDate>Tue, 02 Jul 2013 08:50:30 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/jparallelloops/wiki/Sample%20usage/feed" rel="self" type="application/rss+xml"/><item><title>Sample usage modified by Krzysztof Dębski</title><link>https://sourceforge.net/p/jparallelloops/wiki/Sample%2520usage/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v1
+++ v2
@@ -2,8 +2,6 @@

 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-
-import com.spinn3r.log5j.LogManager;

 import pfor.CompletionCallback;
 import pfor.Merger;
@@ -78,7 +76,5 @@
        /* future.get() will block until the tasks is completed.
         * The result will be the same, as in a completion callback */
        System.out.println("The sum of lengths of all strings: " + future.get());
-
-       LogManager.shutdown();
    }
 }
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Krzysztof Dębski</dc:creator><pubDate>Tue, 02 Jul 2013 08:50:30 -0000</pubDate><guid>https://sourceforge.net5535b6a8cb6f4545581316d4f53aee0d9516ae85</guid></item><item><title>Sample usage modified by Krzysztof Dębski</title><link>https://sourceforge.net/p/jparallelloops/wiki/Sample%2520usage/</link><description>&lt;div class="markdown_content"&gt;&lt;p&gt;package pfor.sample;&lt;/p&gt;
&lt;p&gt;import java.util.concurrent.ExecutionException;&lt;br /&gt;
import java.util.concurrent.Future;&lt;/p&gt;
&lt;p&gt;import com.spinn3r.log5j.LogManager;&lt;/p&gt;
&lt;p&gt;import pfor.CompletionCallback;&lt;br /&gt;
import pfor.Merger;&lt;br /&gt;
import pfor.PArray;&lt;br /&gt;
import pfor.PTask;&lt;br /&gt;
import pfor.ParallelContext;&lt;br /&gt;
import pfor.ProgressCallback;&lt;br /&gt;
import pfor.annotations.Merge;&lt;br /&gt;
import pfor.annotations.Speed;&lt;br /&gt;
import pfor.annotations.Merge.MergeType;&lt;br /&gt;
import pfor.annotations.Speed.SpeedType;&lt;br /&gt;
import pfor.closures.index.tableindex.TableIndexTask;&lt;/p&gt;
&lt;p&gt;public class PForTest {&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;static&lt;/span&gt; &lt;span class="bp"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="err"&gt;[&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt; args) throws InterruptedException, ExecutionException {
    final ParallelContext context = ParallelContext.getDefaultInstance();
    final PArray pArray = context.getPArray();

    /* Input */
    final String&lt;span class="cp"&gt;[]&lt;/span&gt; tab = { &amp;quot;One&amp;quot;, &amp;quot;two&amp;quot;, &amp;quot;three&amp;quot;, &amp;quot;four&amp;quot; };

    /* Action - computing length of a string in an array */
    final TableIndexTask&lt;span class="nt"&gt;&amp;lt;String&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="na"&gt;Integer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt; action = new TableIndexTask&lt;span class="nt"&gt;&amp;lt;String&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt; &lt;span class="na"&gt;Integer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;(tab) {
        /* With 'VERY_SLOW' setting, each subtask will be executed in its own batch (in this example there will be 4 subtasks - one for each string)
         * With 'MODERATE' there would be batches each containing up to 1000 subsequent subtasks.
         * With a lower speed, granuality of subtasks is better, but the overhead of thread congestion and memory usage is also higher
         */
        @Speed(speed = SpeedType.VERY_SLOW)
        public Integer perform(int index) throws Exception {
            return table&lt;span class="cp"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;index&lt;/span&gt;&lt;span class="cp"&gt;]&lt;/span&gt;.length();
        }
    };

    /* Merger of results - order of merging is irrelevant. Random order merger is the fastest, but not always applicable.  */
    final Merger&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt; merger = new Merger&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt;() {
        @Merge(type = MergeType.RANDOM)
        public Integer merge(Integer i1, Integer i2) {
            return i1 + i2;
        }
    };

    /* Creation of task */
    PTask&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt; ptask = pArray.each(action, merger);

    /* Register callback (delivery of a final result on a completion) */
    ptask = ptask.withCompletionCallback(new CompletionCallback&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt;() {
        public void onComplete(Future&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt; future) {
            try {
                System.out.println(&amp;quot;Completion callback received (the result):&amp;quot; + future.get());
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    });

    /* Register progress observer callback (can be used to inform a user how many subtasks have already been completed)
     * Called asynchronously many times during a execution after each batch of tasks (size of a batch is controlled with @Speed annotation */
    ptask = ptask.withProgressCallback(new ProgressCallback() {
        @Override
        public void onProgress(int i) {
            System.out.println(&amp;quot;Progress callback received (number of completed subtasks):&amp;quot; + i);
        }
    });

    /* Execute the task asynchronously */
    final Future&lt;span class="nt"&gt;&amp;lt;Integer&amp;gt;&lt;/span&gt; future = ptask.executeAsynchronously(false);

    /* Give some time for a taks to complete */
    Thread.sleep(100);

    /* future.get() will block until the tasks is completed.
     * The result will be the same, as in a completion callback */
    System.out.println(&amp;quot;The sum of lengths of all strings: &amp;quot; + future.get());

    LogManager.shutdown();
}
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Krzysztof Dębski</dc:creator><pubDate>Tue, 02 Jul 2013 08:38:06 -0000</pubDate><guid>https://sourceforge.net06a77ae5144c4c0208da6573d26c736bf706c2c0</guid></item></channel></rss>