-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread6.java
More file actions
53 lines (46 loc) · 1.63 KB
/
Copy pathThread6.java
File metadata and controls
53 lines (46 loc) · 1.63 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
53
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Test implements Runnable {
private String name;
public Test(String name) {
this.name = name;
}
public void run() {
try {
for (int i = 0; i < 5; i++) {
if (i == 0) {
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println(Thread.currentThread().getName() + " Initialising time for task : " + name + " = " + ft.format(d));
} else {
Date d = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");
System.out.println(Thread.currentThread().getName() + " Execution time for task : " + name + " = " + ft.format(d));
}
Thread.sleep(1000);
}
System.out.println(name + " task completed");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Thread6 {
static final int MAX = 3;
public static void main(String args[]) {
Runnable r1 = new Test("task 1");
Runnable r2 = new Test("task 2");
Runnable r3 = new Test("task 3");
Runnable r4 = new Test("task 4");
Runnable r5 = new Test("task 5");
ExecutorService pool = Executors.newFixedThreadPool(MAX);
pool.execute(r1);
pool.execute(r2);
pool.execute(r3);
pool.execute(r4);
pool.execute(r5);
pool.shutdown();
}
}