-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadReusing.java
59 lines (51 loc) · 2.01 KB
/
ThreadReusing.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
package com.basic.threading;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
* @author rafiul
* @see Generally A Thread will dead after a single Execution
*
* When we have to do n different operation with m different Thread
* where n > m and m >=1, then we need something that will use a single
* thread multiple times for complete all n operations.
* This concept is Thread Pool.
* Where we use a single thread multiple times
*/
class ThreadRunner implements Runnable{
public void run(){
String name = Thread.currentThread().getName();
System.out.printf("%s is starting...\n",name);
System.out.printf("%s is loading 50%%...\n",name);
System.out.printf("complete\n");
}
}
public class ThreadReusing {
public static void main(String[] args) {
/** Creating a Runnable object for create a Thread.
* We will use this single Runnable object for different
* thread in Executor
*/
ThreadRunner runner = new ThreadRunner();
/** Here I have total 4 operation and a single Thread
* So my thread pool use this single in thread for 4 different
* operations and then the thread will dead.
* Here this single thread will run 4 time before dead.
*/
/** After Completed all operations must shutdown the executor **/
ExecutorService singleServer = Executors.newSingleThreadExecutor();
for(int i=0; i<4; i++){
singleServer.execute(runner);
}
singleServer.shutdown();
/** Here I have 2 fixed thread for doing 4 different operations
* So, my thread pool will divide this 4 operations between this
* 2 thread. So each thread will run twice then dead
*/
ExecutorService multiServer = Executors.newFixedThreadPool(2);
for(int i=0; i<4; i++){
multiServer.execute(runner);
}
multiServer.shutdown();
}
}