Repository navigation
Expand file tree
/
Copy pathJavaConcurrencyExample4Class.java
More file actions
53 lines (42 loc) · 1.91 KB
/
Copy pathJavaConcurrencyExample4Class.java
File metadata and controls
53 lines (42 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package JavaConcurrencyExamples1;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Threads are really rather painful to deal with as they're rather low-level API's.
// Java version 5 introduces the Concurrency library. One of the interfaces provided
// is the Executor Service. We can now use the Executor service to manage threads
// rather than calling the Thread class itself.
class WorkerThread implements Runnable {
@Override
public void run() {
try {
String threadName = Thread.currentThread().getName();
System.out.println("Thread " + threadName + " says, foo!");
Thread.sleep(1);
System.out.println("Thread " + threadName + " says, bar!");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public class JavaConcurrencyExample4Class {
public static void main(String[] args) {
// Here we're using the ExecutorService to create a fixed thread pool. We're going to create
// a pool of two threads. Note that the number of tasks can exceed the number of threads in the
// thread pool, in which case they will wait until one of the threads finished their task at which
// time the thread is returned to the thread pool so it can be re-used.
ExecutorService executor = Executors.newFixedThreadPool(2);
// Now iterate over each thread and assign the worker task defined in the worker's run()
// method to the thread and then start the thread.
for (int i=0;i<2;i++) {
Runnable worker = new WorkerThread();
executor.execute(worker);
}
// Here we need to shut down the thread. This is similar to the thread.join() method.
executor.shutdown();
// Wait until all of the threads are terminated.
while (! executor.isTerminated()) {
}
// Done!
System.out.println("All jobs finished!");
}
}