Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Fork and join framework

1,514 views

Published on

The fork/join framework, which is based on the ForkJoinPool class, is an implementation of the Executor interface. It is designed to efficiently run a large number of tasks using a pool of worker threads. A work-stealing technique is used to keep all the worker threads busy, to take full advantage of multiple processors

Published in: Education, Technology
  • Your slides remind me of a class of problem that i got during distributed computing course. For above math-like problem, we did write code for matrix multiplication ( a special case) using RMI. It was merely POC exercise.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • We can apply Fork / Join framework for any problem that can be solved with Divide and Conquer.
    For your problem, it could be solved with Divide and Conquer.
    One question, did you try to write out the code to solve your problem?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Applied to any computation of type

    a_1 * a_2 * ... * a_n

    where * is an associative operator (a*b)*c = a*(b*c)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Fork and join framework

  1. 1. FORK AND JOINFRAMEWORKMinh Tran – 11/2012
  2. 2. Some History• Until recently, most computers only had 1 processing core• Multithreading was simulated on a single core• Not true concurrency• Serial approach to algorithms often sufficed
  3. 3. Multi-core• Concurrency and performance are tied together• Real concurrency – separate threads can execute on different cores at the same time• The JVM runtime controls scheduling
  4. 4. Plain Old ThreadsThread thread = new Thread() { @Override public void run() { System.out.println(">>> I am running in a separate thread!"); }};thread.start();thread.join();
  5. 5. Another example with java.util.concurrentimport java.util.*; public static void main(String[] args) throws Exception {import java.util.concurrent.*; ExecutorService executor = Executors.newFixedThreadPool(2);import static java.util.Arrays.asList; List <Future<Long>> results = executor.invokeAll(asList( new Sum(0, 10), new Sum(100, 1_000), new Sum(10_000,public class Sums { 1_000_000) static class Sum implements ));Callable<Long> { executor.shutdown(); private final long from; private final long to; for (Future<Long> result : results) { Sum(long from, long to) { System.out.println(result.get()); this.from = from; } this.to = to; } } } @Override public Long call() { long acc = 0; for (long i = from; i <= to; i++) { acc = acc + i; } return acc; } }
  6. 6. Fork and Join Framework• Some types of algorithms exist that require tasks to create subtasks and communicate with each other to complete.• Those are the “divide and conquer” algorithms, which are also referred to as “map and reduce,”
  7. 7. Fork and Join Framework (cont.)• Example: partial sums of an Array of Integers• Solving the problem above with executors is easy: • Divide the array into the number n of available physical processing units • Create Callable instances to compute each partial sum • Submit them to an executor managing a pool of n threads • Collect the result to compute the final sum.
  8. 8. Fork and Join Framework (cont.)• The core addition is a new ForkJoinPool executor that is dedicated to running instances implementing ForkJoinTask.• ForkJoinTask objects feature two specific methods: • fork(): allows a ForkJoinTask to be planned for asynchronous execution • join() method allows a ForkJoinTask to wait for the completion of another one.
  9. 9. • There are 2 types of ForkJoinTask: • RecursiveAction represent executions that do not yield a return value. • Instances RecursiveTask yields return values.
  10. 10. Demo / Q&A
  11. 11. Summary• Strong focus on the new fork/join tasks provided by Java SE 7 for making it easier to write parallel programs
  12. 12. Reference• http://www.oracle.com/technetwork/articles/java/fork-join- 422606.html• http://www.ibm.com/developerworks/java/library/j- jtp11137/index.html
  13. 13. Thank You

×