Last active
September 16, 2015 06:46
-
-
Save minheq/e7d0c121dca77f48994e to your computer and use it in GitHub Desktop.
Java Concurrency Practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package aa; | |
import java.util.*; | |
import java.util.concurrent.*; | |
/** | |
* Created by chrisboesch on 15/9/15. | |
*/ | |
public class CallAllSuperWorkers | |
{ | |
public static void main(String[] arg) throws Exception | |
{ | |
//Create the ThreadPool, there are many kinds | |
ForkJoinPool pool = new ForkJoinPool(); | |
//Create a callable worker. | |
Callable<String> worker1 = new SuperWorker("Worker 1"); | |
//Create any array of workers to execute together. | |
ArrayList tasks = new ArrayList(); | |
tasks.add(new SuperWorker("Worker 2")); | |
tasks.add(new SuperWorker("Worker 3")); | |
tasks.add(new SuperWorker("Worker 6")); | |
tasks.add(new SuperWorker("Worker 7")); | |
//Make a RecursiveAction worker. | |
RecursiveAction worker = new SuperWorker("Worker 4"); | |
//Make a thread to run another worker. | |
Thread t = new Thread(new SuperWorker("Worker 5")); | |
Thread t2 = new Thread(new SuperWorker("Worker 8")); | |
// Now we start everything. | |
t.start(); //start the thread which executes run() | |
t2.start(); | |
Future<String> future = pool.submit(worker1); //start the first callable worker. | |
List<Future<String>> results = pool.invokeAll(tasks); //start all the workers in the list. | |
System.out.println("THE CLIENT CODE"); // do something else. | |
//Wait for all the workers in the list to finish. | |
for (int i = 0; i < tasks.size(); i++) | |
{ | |
String returnedValue = results.get(i).get(); //wait for each call() method to finish. | |
System.out.println(returnedValue); | |
} | |
System.out.println("All call() methods in the list of callable workers finished."); | |
future.get(); // wait for worker1 to finish. | |
System.out.println("The call() method for worker1 finished."); | |
t.join(); //wait for run() to finish in the thread. | |
t2.join(); //wait for run() to finish in the thread. | |
System.out.println("Threads has been joined."); | |
pool.invoke(worker); //waith for compute() to start and finish. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Worker 5 in run() because of Thread(worker).start() | |
Worker 1 in call() because of pool.submit(worker) | |
Worker 8 in run() because of Thread(worker).start() | |
Worker 2 in call() because of pool.submit(worker) | |
Worker 3 in call() because of pool.submit(worker) | |
Worker 6 in call() because of pool.submit(worker) | |
Worker 7 in call() because of pool.submit(worker) | |
THE CLIENT CODE | |
Hello from Worker 2 | |
Hello from Worker 3 | |
Hello from Worker 6 | |
Hello from Worker 7 | |
All call() methods in the list of callable workers finished. | |
The call() method for worker1 finished. | |
Threads has been joined. | |
Create two new Workers | |
copy_1 in compute() because of pool.invoke(worker) | |
copy_2 in compute() because of pool.invoke(worker) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.*; | |
/** | |
* Created by chrisboesch on 15/9/15. | |
*/ | |
public class SuperWorker extends RecursiveAction implements Runnable, Callable<String> | |
{ | |
public String name; | |
public SuperWorker(String _name) { | |
name = _name; | |
} | |
//New Thread(worker).start() would execute run() | |
public void run() | |
{ | |
System.out.println(name+" in run() because of Thread(worker).start() "); | |
} | |
//Future<String> future = pool.submit(new CallableWorker()) would execute call() | |
public String call() throws Exception | |
{ | |
System.out.println(name + " in call() because of pool.submit(worker) "); | |
return "Hello from " + name; | |
} | |
//pool.invoke(worker) would execute compute() | |
protected void compute() { | |
if(!name.contains("copy")) { | |
System.out.println("Create two new Workers"); | |
SuperWorker worker1 = new SuperWorker("copy_1"); | |
SuperWorker worker2 = new SuperWorker("copy_2"); | |
invokeAll(worker1, worker2); | |
} else { | |
System.out.println(name + " in compute() because of pool.invoke(worker) "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment