-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool1.java
More file actions
40 lines (39 loc) · 894 Bytes
/
ThreadPool1.java
File metadata and controls
40 lines (39 loc) · 894 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
import java.util.*;
import java.util.concurrent.*;
class Runner implements Runnable
{
private int id;
public Runner(int id)
{
this.id=id;
}
public void run()
{
System.out.println("Hello "+id+" Instance ");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println("Bye "+id+" Instance ");
}
}
public class ThreadPool1
{
public static void main(String[] args)
{
ExecutorService exc=Executors.newFixedThreadPool(3); //ThreadPool of 3 threads
for(int i=0;i<10;i++)
{
exc.submit(new Runner(i));
}
System.out.println("All tasks submitted");
exc.shutdown();
System.out.println("Done with Tasks");
try{exc.awaitTermination(4,TimeUnit.MINUTES);} //Awaits only for this given time
catch(Exception e){}
}
}