1 Apr 5, 2012 ForkJoin New in Java 7. Incrementor I package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask;

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Concurrency 101 Shared state. Part 1: General Concepts 2.
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 1 Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Last.
CSE332: Data Abstractions Lecture 18: Introduction to Multithreading and Fork-Join Parallelism Dan Grossman Spring 2010.
Access to Names Namespaces, Scopes, Access privileges.
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Lecture 20: Parallelism & Concurrency CS 62 Spring 2013 Kim Bruce & Kevin Coogan CS 62 Spring 2013 Kim Bruce & Kevin Coogan Some slides based on those.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Exercise 1 : ex1.java class C extends Thread { int i; C(int i) { this.i = i; } public void run() { System.out.println("Thread " + i + " says hi"); System.out.println("Thread.
Methods We write methods in our programs for many reasons:
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Multithreading in JAVA
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
CSE332: Data Abstractions Lecture 19: Introduction to Multithreading and Fork-Join Parallelism Tyler Robison Summer
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Classes - Intermediate
Methods.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Parallelism idea Example: Sum elements of a large array Idea: Have 4 threads simultaneously sum 1/4 of the array –Warning: This is an inferior first approach.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Multi Threading.
The Lifetime of a Variable
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Instructor: Lilian de Greef Quarter: Summer 2017
CSE 332: Analysis of Fork-Join Parallel Programs
CSE 332: Intro to Parallelism: Multithreading and Fork-Join
Namespaces, Scopes, Access privileges
A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 1 Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Last.
Multithreaded Programming in Java
Class Structure 16-Nov-18.
ForkJoin New in Java 7 Apr 5, 2012.
CSE332: Data Abstractions Lecture 18: Introduction to Multithreading & Fork-Join Parallelism Dan Grossman Spring 2012.
Class Structure 28-Nov-18.
Class Structure 7-Dec-18.
ForkJoin New in Java 7 Apr 5, 2012.
Multithreaded Programming
Class Structure 2-Jan-19.
Parallelism for summation
class PrintOnetoTen { public static void main(String args[]) {
Namespaces, Scopes, Access privileges
Class Structure 25-Feb-19.
Constructors, GUI’s(Using Swing) and ActionListner
Threads in Java James Brucker.
More on Creating Classes
Developing Java Applications with NetBeans
Exercise 1 : ex1.java Thread Creation and Execution
Developing Java Applications with NetBeans
Exercise 1 : ex1.java Thread Creation and Execution (sleep() and join()) class C extends Thread {   int i;   C(int i) { this.i = i; }   public void run()
Parallel Programming with ForkJoinPool Tasks in Java
ITE “A” GROUP 2 ENCAPSULATION.
CMSC 202 Threads.
Parallel Programming with ForkJoinPool Tasks in Java
Presentation transcript:

1 Apr 5, 2012 ForkJoin New in Java 7

Incrementor I package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; 2 The first thing you need to do is to import some classes. These are built into Java 7, but are also available as a separate download for Java 6.

Incrementor II package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Incrementor extends RecursiveTask { } 3 Next you need to create a class that extends RecursiveTask. A RecursiveTask is like a Thread, but you can retrieve a value from it after it finishes. Supply a type parameter (Integer, in this example) to specify the kind of value you want.

Incrementor III package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Incrementor extends RecursiveTask { public static ForkJoinPool fjPool = new ForkJoinPool(); } 4 Create a ForkJoinPool. Create only one of these, and it should be static. A ForkJoinPool is a pool of Threads. Java will take care of all the thread allocation and deallocation for you.

Incrementor IV package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Incrementor extends RecursiveTask { public static ForkJoinPool fjPool = new ForkJoinPool(); int theNumber; Incrementor(int x) { theNumber = x; } public Integer compute() { return theNumber + 1; } public static void main(String[] args) { int fortyThree = fjPool.invoke(new Incrementor(42)); Systemout.println(fortyThree); } } 5 Make an instance of our Incrementor class. We defined it to have a “ generic ” type parameter, so let's define a field “ theNumber ” to hold it. In this example, our constructor sets theNumber to have an initial value.

Incrementor V package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Incrementor extends RecursiveTask { public static ForkJoinPool fjPool = new ForkJoinPool(); int theNumber; Incrementor(int x) { theNumber = x; } public Integer compute() { return theNumber + 1; } public static void main(String[] args) { int fortyThree = fjPool.invoke(new Incrementor(42)); System.out.println(fortyThree); } } 6 Define a compute() method. compute() is what we use in place of run().

Incrementor VI package helloWorld; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Incrementor extends RecursiveTask { public static ForkJoinPool fjPool = new ForkJoinPool(); int theNumber; Incrementor(int x) { theNumber = x; } public Integer compute() { return theNumber + 1; } public static void main(String[] args) { int fortyThree = fjPool.invoke(new Incrementor(42)); System.out.println(fortyThree); } } 7 Finally, create a new Incrementor (like getting a Thread, but with a parameter), and tell the ForkJoinPool to invoke it (like calling its “ start ” method). Calling “ invoke ” will cause “ run ” to be executed.

What did that code do? int fortyThree = fjPool.invoke(new Incrementor(42)); How is this any different from int fortyThree = new Incrementor(42).compute(); ? Answer: compute runs as a RecursiveTask (because class Incrementor extends RecursiveTask ) So what? RecursiveTasks are “ lightweight ” Threads (you can have up to of them) which can fork and join public final ForkJoinTask fork() arranges to asynchronously execute this task. Any given task should only be forked once. public final V join(), unlike the Thread join, returns a result.

Summing an array I: Context import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveTask; class Globals { static ForkJoinPool fjPool = new ForkJoinPool(); } class Sum extends RecursiveTask { protected Long compute() {…} static long sumArray(int[] array) {…} } 9 We only ever need or want one ForkJoinPool: Let ’ s make a RecursiveTask/compute() combination:

Summing an array II: Parameters class Sum extends RecursiveTask { static final int SEQUENTIAL_THRESHOLD = 5000; int low; int high; int[] array; Sum(int[] arr, int lo, int hi) { array = arr; low = lo; high = hi; } protected Long compute() {…} static long sumArray(int[] array) {…} } 10 We need to pass parameters in to compute(). But compute() doesn ’ t take parameters! What do we do? Notice the constructor !

Summing an array III: Doing the work class Sum extends RecursiveTask { protected Long compute() { if(high - low <= SEQUENTIAL_THRESHOLD) { 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 rightAns = right.compute(); long leftAns = left.join(); return leftAns + rightAns; } 11 Not much to do? Do it here. Split the array into two parts. Start another thread to sum the left half. I ’ ll do the right half myself. When I ’ m done, wait for the other thread.

12 Let ’ s see that again We create two new Sums, one to fork and one to use ourself Sum left = new Sum(array, low, mid); Sum right = new Sum(array, mid, high); We fork to sum the left half of the array left.fork(); We sum the right half ourselves, with a plain old method call (not an additional Thread) that returns a result long rightAns = right.compute(); When we are done, the fork may or may not be done. We wait for it. When it is done, we get its result long leftAns = left.join(); Then, of course, we add the two results return leftAns + rightAns; 12

Summing an array IV: Begin here class Sum extends RecursiveTask { // other methods that we have been looking at static long sumArray(int[] array) { return Globals.fjPool.invoke(newSum(array,0,array.length)); } } The call to fjPool.invoke creates an instance of this Sum class, which is a RecursiveTask, and that gets the ball rolling. invoke should not be called from within a RecursiveTask or RecursiveAction. It should only be called from sequential code. 15

14 Running the example import java.util.Random; public class SumTester { static Random rand = new Random(); public static void main(String[] args) { int[] array = new int[100000]; for (int i = 0; i < array.length; i += 1) { array[i] = rand.nextInt(11); } long sum = Sum.sumArray(array); System.out.println(sum); }

15 The End