DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. An Introduction to the Fork / Join Framework

An Introduction to the Fork / Join Framework

Ricardo Zuasti user avatar by
Ricardo Zuasti
·
Apr. 10, 12 · Interview
Like (2)
Save
Tweet
Share
22.24K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous posts about the java.util.concurrent framework, I introduced what for me is the core of the package and what you will use the most on real-world applications.

Going further with the topic, it’s worth to know the new fork/join framework introduced in Java 7. The pros and cons of this framework over the usage of thread pools or regular thread/runnable development has been subject of a lot of debate, but I think it is still a nice tool to have in your arsenal.

I’ll use a more classic approach to show you the basic structure of the fork/join framework and code a version of the Quicksort algorithm using it.

My goal here is to introduce you to the tools provided by the fork/join structure in Java 7, this is not a discussion about the best Quicksort implementation. If you want to delve into implementing the best Quicksort possible I recommend you check out this great post about it.

The fork/join framework provides a very straightforward and intuitive structure to implement recursive and divide and conquer problems that can be solved concurrently. You start with the big problem and break it down in smaller work units until each work unit can be solved directly.

The implementation of your problem solver must be a subclass of ForkJoinTask, and you will most likely implement a subclass of it’s children RecursiveAction or RecursiveTask; RecursiveAction represents a recursive problem solver, capable of solving a small enough problem directly or breaking it down into smaller work units; RecursiveTask is similar, but it also has the capability of returning a result for each work unit executed.

To implement our Quicksort we will use the in-place strategy (we are hardcore, right?) and use a generic list as data so it supports any Java Comparable. With these premises our task can be coded as:

package com.ricardozuasti;
 
import java.util.List;
import java.util.concurrent.RecursiveAction;
 
public class QuickSort<T extends Comparable> extends RecursiveAction {
 
    private List<T> data;
    private int left;
    private int right;
 
    public QuickSort(List<T> data){
        this.data=data;
        this.left = 0;
        this.right = data.size() - 1;
    }
 
    public QuickSort(List<T> data, int left, int right){
        this.data = data;
        this.left = left;
        this.right = right;
    }
 
    @Override
    protected void compute() {
        if (left < right){
            int pivotIndex = left + ((right - left)/2);
 
            pivotIndex = partition(pivotIndex);
 
            invokeAll(new QuickSort(data, left, pivotIndex-1),
                      new QuickSort(data, pivotIndex+1, right));
        }
 
    }
 
    private int partition(int pivotIndex){
        T pivotValue = data.get(pivotIndex);
 
        swap(pivotIndex, right);
 
        int storeIndex = left;
        for (int i=left; i<right; i++){
            if (data.get(i).compareTo(pivotValue) < 0){
                swap(i, storeIndex);
                storeIndex++;
            }
        }
 
        swap(storeIndex, right);
 
        return storeIndex;
    }
 
    private void swap(int i, int j){
        if (i != j){
            T iValue = data.get(i);
 
            data.set(i, data.get(j));
            data.set(j, iValue);
        }
    }
}

The compute() method is the one that the fork/join framework will invoke for each work unit. Our base step (small work unit) is to do nothing (since a size 1 list is always sorted). Our recursive step uses the invokeAll() method, which is a shortcut to fork() all small work units and then join() them all again.

And that’s it! We don’t really need anything else. To actually use our new concurrent Quicksort we use a ForkJoinPool instance:

package com.ricardozuasti;
 
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
 
public class Concurrency3 {
    public static void main(String[] args) {
        final int SIZE = 10000;
 
        List<Integer> myList = new ArrayList<Integer>(SIZE);
 
        for (int i=0; i<SIZE; i++){
            int value = (int) (Math.random() * 100);
            myList.add(value);
        }
 
        QuickSort<Integer> quickSort = new QuickSort<Integer>(myList);
 
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(quickSort);
    }
}

And again the invoke method is equivalent to doing a fork() followed by a join(), so after the pool.invoke() method returns our list is sorted.

 

 

Framework Fork (software development)

Published at DZone with permission of Ricardo Zuasti, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java
  • Simulating and Troubleshooting BLOCKED Threads in Kotlin [Video]
  • How To Use Linux Containers
  • DevOps for Developers — Introduction and Version Control

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: