-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathThreadPoolDemo.java
More file actions
49 lines (38 loc) · 851 Bytes
/
ThreadPoolDemo.java
File metadata and controls
49 lines (38 loc) · 851 Bytes
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
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class Tasks implements Runnable{
private int taskno;
public Tasks(int taskno) {
// TODO Auto-generated constructor stub
this.taskno=taskno;
}
@Override
public void run() {
for(int i=0;i==100;i+=25)
{
String name=Thread.currentThread().getName();
System.out.println(name + "completed task :"+ taskno +" by " + i + "percent.");
try {
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
}
}
}
}
public class ThreadPoolDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ExecutorService es=Executors.newFixedThreadPool(2);
//create 4 task
Tasks t[]=new Tasks[4];
for(int i=0;i<4;i++)
{
t[i]=new Tasks(i);
System.out.println(i);
es.execute(t[i]);
}
es.shutdown();
}
}