Java Concurrency Fork-Join framework
w3hello.com logo
Java Basics
Java Introduction
Java Installation
Java Operators
Java Variable Types
Java Loops
Java For Loop
Java While Loop
Java do while loop
Java Continue
Java JButton
Java JTextArea
Java Logical Operators
switch statement in java
Java JPasswordField
Java Year
Java Constructor
Java Basic Syntax
Java JComboBox
Java JToolBar
Java if Statements
Java JMenuBar, JMenu and JMenuItem
Java JDialog
Java JToggleButton
Java JFrame
Java Zip Exceptions
Java DIP - Enhancing Image Brightness
Java JFileChooser
Java Concurrency ExecutorService Interface
Java throw exception
LinkedHashMap in Java
Java Digital Watch
Java instanceof
Java AWT Button
Java For Complete Beginners - loops and arrays
Array vs ArrayList in Java
java.util.Calendar
Java JColorChooser
Java URL Processing
Guide to Java Exception Hierarchy
Java Concurrency ConcurrentMap Interface
Synchronization in Java
Java Internationalization
Java Fetch Image from Oracle Database
Unicode System in Java
Java ActionListener
java.lang.reflect - Interfaces
Java Thread Pool | Thread Pooling in Java
Java Graphics in Applet
Java Convert int to long
Java JSpinner
Java Primitive data type
Java Numeric Literals with Underscore
Inter-thread communication in Java
The Tetris game in Java Swing
Java Applet Basics
Java Convert Binary to Decimal
Static keyword in Java
Java Convert Hex to Decimal
Java JRadioButton
Java XML Overview
Java Concurrency Quick Guide
Java Month enum
Types of Exceptions
Reentrant Monitor in Java
Java Loop Control
Java Access Modifiers
WebSocket in Jetty
Transaction Management in Java JDBC
Java SimpleDateFormat
Java 7 JDBC Improvement
Java FlowLayout
Java AWT Scrollbar
Java Decision Making
Java Database Connectivity with Oracle
Java ZoneId
Java else-if Statements
Java DIP - Laplacian Operator
Top Java Books
Java Date Time - Custom Date Format Patterns
Java static and this Keyword
Java JList
Java Concurrency Lock Interface
Java LocalDate
Creating a thread in Java
Jetty's HTTPClient
Java Package
Displaying image in Applet
Java Overriding
Java AWT Choice
Java SAX Parser Overview
Java Save File in Oracle Database
Java Modifier
Java JDesktopPane
Java Inheritance & Polymorphism
Discuss Java Concurrency
this keyword in java
Java KeyListener
Java Online Training
Java iText
History of Java
Java Basics
Previous : Learn Java Encapsulation in 10 Minutes Next : Java Throws Keyword

Java Concurrency Fork-Join framework


The fork-join framework allows to break a certain task on several workers and then wait for the result to combine them. It leverages multi-processor machine's capacity to great extent. Following are the core concepts and objects used in fork-join framework.

Fork

Fork is a process in which a task splits itself into smaller and independent sub-tasks which can be executed concurrently.

Syntax

Sum left  = new Sum(array, low, mid);
left.fork();

Here Sum is a subclass of RecursiveTask and left.fork() spilts the task into sub-tasks.

Join

Join is a process in which a task join all the results of sub-tasks once the subtasks have finished executing, otherwise it keeps waiting.

Syntax

left.join();

Here left is an object of Sum class.

ForkJoinPool

it is a special thread pool designed to work with fork-and-join task splitting.

Syntax

ForkJoinPool forkJoinPool = new ForkJoinPool(4);

Here a new ForkJoinPool with a parallelism level of 4 CPUs.

RecursiveAction

RecursiveAction represents a task which does not return any value.

Syntax

class Writer extends RecursiveAction {
   @Override
   protected void compute() { }
}

RecursiveTask

RecursiveTask represents a task which returns a value.

Syntax

class Sum extends RecursiveTask<Long> {
   @Override
   protected Long compute() { return null; }
}

Example

The following TestThread program shows usage of Fork-Join framework in thread based environment.

Live Demo
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class TestThread {

   public static void main(final String[] arguments) throws
InterruptedException, 
      ExecutionException {
      
      int nThreads = Runtime.getRuntime().availableProcessors();
      System.out.println(nThreads);
      
      int[] numbers = new int[1000]; 

      for(int i = 0; i < numbers.length; i++) {
         numbers[i] = i;
      }

      ForkJoinPool forkJoinPool = new ForkJoinPool(nThreads);
      Long result = forkJoinPool.invoke(new
Sum(numbers,0,numbers.length));
      System.out.println(result);
   }  

   static class Sum extends RecursiveTask<Long> {
      int low;
      int high;
      int[] array;

      Sum(int[] array, int low, int high) {
         this.array = array;
         this.low   = low;
         this.high  = high;
      }

      protected Long compute() {
         
         if(high - low <= 10) {
            long sum = 0;
            
            for(int i = low; i < high; ++i) 
               sum += array[i];
               return sum;
         } else {	    	
            int mid = low + (high - low) / 2;
            Sum left  = new Sum(array, low, mid);
            Sum right = new Sum(array, mid, high);
            left.fork();
            long rightResult = right.compute();
            long leftResult  = left.join();
            return leftResult + rightResult;
         }
      }
   }
}

This will produce the following result.

Output

32
499500
Previous : Learn Java Encapsulation in 10 Minutes Next : Java Throws Keyword
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap