-
Notifications
You must be signed in to change notification settings - Fork 5
/
ThreadPool.java
93 lines (80 loc) · 2.54 KB
/
ThreadPool.java
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package multicupteas.concurrency;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
public static void main(String[] args) {
//To match the output of the post, call the following methods onw by one by commenting other.
// newSingleThreadExecutor();
// newFixedThreadPool();
// newCachedThreadPool();
}
static void pring5Times(String statement) {
for (int i = 0; i < 5; i++) {
System.out.println(statement + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* usage of Executors.newSingleThreadExecutor().
* This will reuse the single thread to all the task.
*/
static void newSingleThreadExecutor() {
System.out.println("Running Executors.newSingleThreadExecutor()");
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
singleThreadPool.execute(new Car1());
// bike and boat will wait till car driving is completed.
singleThreadPool.execute(new Bike1());
singleThreadPool.execute(new Boat1());
//make sure to shitdown.
singleThreadPool.shutdown();
}
/**
*usage of Executors.newFixedThreadPool(int size)
*
* This will use the 2 threads at the same time.
*/
static void newFixedThreadPool(){
System.out.println("Running Executors.newFixedThreadPool(2)");
ExecutorService fixedSizeThreadPool = Executors.newFixedThreadPool(2);
fixedSizeThreadPool.execute(new Car1());
fixedSizeThreadPool.execute(new Bike1());
// this time Since threadpool only has 2 thread,
// Boat will wait till car driving and bike riding is completed.
fixedSizeThreadPool.execute(new Boat1());
fixedSizeThreadPool.shutdown();
}
/**
* usage of Executors.newCachedThreadPool()
* Thread assignment will not wait for a thread to availability, It will create a new one if not available.
*/
static void newCachedThreadPool(){
System.out.println("Running Executors.newCachedThreadPool()");
ExecutorService chachedThreadPool = Executors.newCachedThreadPool();
chachedThreadPool.execute(new Car1());
chachedThreadPool.execute(new Bike1());
chachedThreadPool.execute(new Boat1());
chachedThreadPool.shutdown();
}
}
class Car1 implements Runnable {
@Override
public void run() {
ThreadPool.pring5Times("I am driving a Car on road ");
}
}
class Bike1 implements Runnable {
@Override
public void run() {
ThreadPool.pring5Times("I am riding a Bike on road ");
}
}
class Boat1 implements Runnable {
@Override
public void run() {
ThreadPool.pring5Times("I am Sailing the boat on sea ");
}
}