-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolTest.java
More file actions
35 lines (29 loc) · 942 Bytes
/
ThreadPoolTest.java
File metadata and controls
35 lines (29 loc) · 942 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
import java.util.concurrent.*;
class MyTask implements Runnable {
private String name;
public MyTask(String name) {
this.name = name;
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
@Override
public void run() {
try {
System.out.println("실행중 : " +name);
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadPoolTest {
public static void main(String[] args) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
for(int i = 1; i <= 5; i++) {
MyTask task = new MyTask("작업" + i);
System.out.println("작업 생성 : " + task.getName());
executor.execute(task);
}
executor.shutdown();
}
}