fork and join framework

13
FORK AND JOIN FRAMEWORK Minh Tran – 11/2012

Upload: minh-tran

Post on 26-May-2015

594 views

Category:

Education


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Fork and join framework

FORK AND JOIN FRAMEWORKMinh Tran – 11/2012

Page 2: Fork and join framework

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

Page 3: Fork and join framework

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

Page 4: Fork and join framework

Plain Old ThreadsThread thread = new Thread() {

@Override public void run() {

System.out.println(">>> I am running in a separate thread!");

}

};

thread.start();

thread.join();

Page 5: Fork and join framework

Another example with java.util.concurrentimport java.util.*;

import java.util.concurrent.*;

import static java.util.Arrays.asList;

public class Sums {

static class Sum implements Callable<Long> {

private final long from;

private final long to;

Sum(long from, long to) {

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;

}

}

public static void main(String[] args) throws Exception {

ExecutorService executor = Executors.newFixedThreadPool(2);

List <Future<Long>> results = executor.invokeAll(asList(

new Sum(0, 10), new Sum(100, 1_000), new Sum(10_000, 1_000_000)

));

executor.shutdown();

for (Future<Long> result : results) {

System.out.println(result.get());

}

}

}

Page 6: Fork and join framework

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,”

Page 7: Fork and join framework

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.

Page 8: Fork and join framework

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.

Page 9: Fork and join framework

• There are 2 types of ForkJoinTask:• RecursiveAction represent executions that do not yield a return

value.• Instances RecursiveTask yields return values.

Page 10: Fork and join framework

Demo / Q&A

Page 11: Fork and join framework

Summary• Strong focus on the new fork/join tasks provided by Java

SE 7 for making it easier to write parallel programs

Page 13: Fork and join framework

Thank You