SlideShare a Scribd company logo
FORK AND JOIN
FRAMEWORK
Minh Tran – 11/2012
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
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
Plain Old Threads
Thread thread = new Thread() {
   @Override public void run() {
     System.out.println(">>> I am running in a separate thread!");
   }
};
thread.start();
thread.join();
Another example with java.util.concurrent
import 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;
     }
  }
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,”
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.
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.
• There are 2 types of ForkJoinTask:
  • RecursiveAction represent executions that do not yield a return
    value.
  • Instances RecursiveTask yields return values.
Demo / Q&A
Summary
• Strong focus on the new fork/join tasks provided by Java
 SE 7 for making it easier to write parallel programs
Reference
• http://www.oracle.com/technetwork/articles/java/fork-join-
  422606.html
• http://www.ibm.com/developerworks/java/library/j-
  jtp11137/index.html
Thank You

More Related Content

What's hot (20)

PPT
Introduction to ADO.NET
rchakra
 
PPT
Object-oriented concepts
BG Java EE Course
 
PPT
State Diagrams
Vaidik Trivedi
 
PPT
C++ Language
Syed Zaid Irshad
 
PPTX
Client side scripting and server side scripting
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Webservices
Gerard Sylvester
 
PPT
Basic concepts of object oriented programming
Sachin Sharma
 
PDF
4.3 MySQL + PHP
Jalpesh Vasa
 
PPTX
Internship presentation
Wasim Shemna
 
PPTX
C# programming language
swarnapatil
 
ODP
Introduction to Shell script
Bhavesh Padharia
 
PPT
Object and class relationships
Pooja mittal
 
PPSX
Functional dependency
Dashani Rajapaksha
 
PPT
C#.NET
gurchet
 
PPTX
C# File IO Operations
Prem Kumar Badri
 
PPTX
Introduction to Object Oriented Programming
Moutaz Haddara
 
PDF
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
PPT
Testing under cloud
gaurav jain
 
PPTX
interface in c#
Deepti Pillai
 
Introduction to ADO.NET
rchakra
 
Object-oriented concepts
BG Java EE Course
 
State Diagrams
Vaidik Trivedi
 
C++ Language
Syed Zaid Irshad
 
Client side scripting and server side scripting
baabtra.com - No. 1 supplier of quality freshers
 
Webservices
Gerard Sylvester
 
Basic concepts of object oriented programming
Sachin Sharma
 
4.3 MySQL + PHP
Jalpesh Vasa
 
Internship presentation
Wasim Shemna
 
C# programming language
swarnapatil
 
Introduction to Shell script
Bhavesh Padharia
 
Object and class relationships
Pooja mittal
 
Functional dependency
Dashani Rajapaksha
 
C#.NET
gurchet
 
C# File IO Operations
Prem Kumar Badri
 
Introduction to Object Oriented Programming
Moutaz Haddara
 
OO Design and Design Patterns in C++
Ganesh Samarthyam
 
Testing under cloud
gaurav jain
 
interface in c#
Deepti Pillai
 

Similar to Fork and join framework (20)

ODP
Concurrent Programming in Java
Ruben Inoto Soto
 
PDF
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
PDF
The Future of Futures - A Talk About Java 8 CompletableFutures
Haim Yadid
 
PDF
Java fork join
Masud Hasan
 
PDF
Fork Join (BeJUG 2012)
Sander Mak (@Sander_Mak)
 
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
PDF
Java concurrency
Abhijit Gaikwad
 
PDF
A Java Fork_Join Framework
Hiroshi Ono
 
PDF
Groovy concurrency
Alex Miller
 
PPT
Parallel programming
Swain Loda
 
KEY
Modern Java Concurrency
Ben Evans
 
PPTX
Concurrent talk
rahulrevo
 
PDF
Java 7 LavaJUG
julien.ponge
 
ODP
Pick up the low-hanging concurrency fruit
Vaclav Pech
 
PDF
Loom and concurrency latest
Srinivasan Raghavan
 
PPT
cs2110Concurrency1.ppt
narendra551069
 
PDF
Java Tutorials - Concurrency
Christian Rubiales
 
PPTX
parallel-asynchronous-programming-java.pptx
2022ac05156
 
ODP
GPars: Groovy Parallelism for Java
Russel Winder
 
Concurrent Programming in Java
Ruben Inoto Soto
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
julien.ponge
 
The Future of Futures - A Talk About Java 8 CompletableFutures
Haim Yadid
 
Java fork join
Masud Hasan
 
Fork Join (BeJUG 2012)
Sander Mak (@Sander_Mak)
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
Mario Fusco
 
Java concurrency
Abhijit Gaikwad
 
A Java Fork_Join Framework
Hiroshi Ono
 
Groovy concurrency
Alex Miller
 
Parallel programming
Swain Loda
 
Modern Java Concurrency
Ben Evans
 
Concurrent talk
rahulrevo
 
Java 7 LavaJUG
julien.ponge
 
Pick up the low-hanging concurrency fruit
Vaclav Pech
 
Loom and concurrency latest
Srinivasan Raghavan
 
cs2110Concurrency1.ppt
narendra551069
 
Java Tutorials - Concurrency
Christian Rubiales
 
parallel-asynchronous-programming-java.pptx
2022ac05156
 
GPars: Groovy Parallelism for Java
Russel Winder
 
Ad

Recently uploaded (20)

PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Ad

Fork and join framework

  • 1. FORK AND JOIN FRAMEWORK Minh Tran – 11/2012
  • 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. 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. Plain Old Threads Thread thread = new Thread() { @Override public void run() { System.out.println(">>> I am running in a separate thread!"); } }; thread.start(); thread.join();
  • 5. Another example with java.util.concurrent import 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. 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. 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. 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. • There are 2 types of ForkJoinTask: • RecursiveAction represent executions that do not yield a return value. • Instances RecursiveTask yields return values.
  • 11. Summary • Strong focus on the new fork/join tasks provided by Java SE 7 for making it easier to write parallel programs
  • 12. Reference • http://www.oracle.com/technetwork/articles/java/fork-join- 422606.html • http://www.ibm.com/developerworks/java/library/j- jtp11137/index.html

Editor's Notes

  • #5: All the code in this example does is create a thread that prints a string to the standard output stream.The main thread waits for created (child) thread to complete by calling join().Directly manipulating threads this way is fine for simple examples, but with concurrent programming, such code can quickly become error-prone, especially when several threads need to cooperate to perform a larger task. In such cases, their control flow needs to be coordinated.For example, the completion of a thread’s execution might depend on other threads having completed their execution.The usual well-known example is that of the producer/consumer, because the producer should wait for the consumer if the consumer’s queue is full, and the consumer should wait for the producer when empty.This requirement can be addressed through shared state and condition queues, but you still have to use synchronization byusing java.lang.Object.notify() and java.lang.Object.wait() on shared-state objects, which is easy to get wrong.Finally, a common pitfall is to use synchronize and provide mutual exclusion over large pieces of code or even whole methods. While this approach leads to thread-safe code, it usually yields poor performance due to the limited parallelism that is induced by exclusion being in effect too long.As is often the case in computing, manipulating low-level primitives to implement complex operations opens the door to mistakes, and as such, developers should seek to encapsulate complexity within efficient, higher-level libraries. Java SE 5 provided us with just that ability.
  • #6: This example program leverages an executor to compute sums of long integers.The Sum class implements the Callable interface that is used by executors for result-bearing computations, and the concurrent work is performed within the call() method. The java.util.concurrent.Executors class provides several utility methods, such as providing pre-configured executors or wrapping plain old java.lang.Runnable objects into instances of Callable.The advantage of using Callable over Runnable is that Callable can explicitly return a value.This example uses an executor that dispatches work over two threads. The ExecutorService.invokeAll() method takes a collection of Callable instances and waits for the completion of all of them before returning.It returns a list of Future objects, which all represent the “future” result of the computation. If we were to work in an asynchronous fashion, we could test each Future object to check whether its corresponding Callable has finished its work and check whether it threw an exception, and we could even cancel it.By contrast, when using plain old threads, you must encode cancellation logic through a shared mutable Boolean and cripple the code with periodic checks over this Boolean.Because invokeAll() is blocking, we can directly iterate over the Future instances and fetch their computed sums.Also note that an executor service must be shut down. If it is not shut down, the Java Virtual Machine will not exit when the main method does, because there will still be active threads around.
  • #7: Executors are a big step forward compared to plain old threads because executors ease the management of concurrent tasks.The idea is to split the data space to be processed by an algorithm into smaller, independent chunks. That is the “map” phase. In turn, once a set of chunks has been processed, partial results can be collected to form the final result. This is the “reduce” phase.
  • #8: However, the execution plan often is not so simple. In particular, the “map” phase that identifies chunks of data “small enough” to be processed independently in an efficient manner does not know the data space topology in advance. This is especially true for graph-based and tree-based data structures.In those cases, algorithms should create hierarchies of “divisions,” waiting for subtasks to complete before returning a partial result. Although less optimal in an array like the one in Figure 1, several levels of concurrent partial-sum computations can be used (for example, divide the array into four subtasks on a dual-core processor).The problem with the executors for implementing divide and conquer algorithms is not related to creating subtasks, because a Callable is free to submit a new subtask to its executor and wait for its result in a synchronous or asynchronous fashion. The issue is that of parallelism: When a Callable waits for the result of another Callable, it is put in a waiting state, thus wasting an opportunity to handle another Callable queued for execution.
  • #9: The fork/join framework added to the java.util.concurrent package in Java SE 7 through Doug Lea’s efforts fills that gap. The Java SE 5 and Java SE 6 versions of java.util.concurrent helped in dealing with concurrency, and the additions in Java SE 7 help with parallelism.