-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinMainV3.java
More file actions
54 lines (41 loc) · 1.41 KB
/
Copy pathJoinMainV3.java
File metadata and controls
54 lines (41 loc) · 1.41 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
54
package thread.control.join;
import util.MyLogger;
import util.ThreadUtils;
public class JoinMainV3 {
public static void main(String[] args) throws InterruptedException {
MyLogger.log("Start()");
SumTask task1 = new SumTask(1,50);
SumTask task2 = new SumTask(51,100);
Thread thread1 = new Thread(task1, "thread-1");
Thread thread2 = new Thread(task2, "thread-2");
thread1.start();
thread2.start();
MyLogger.log("main Thread watting for thread1, thread2 end");
thread1.join();
thread2.join();
MyLogger.log("main Thread watting end");
MyLogger.log("task1 result = " + task1.result);
MyLogger.log("task2 result = " + task2.result);
int sumAll = task1.result + task2.result;
MyLogger.log("sumAll = " + sumAll);
MyLogger.log("End()");
}
static class SumTask implements Runnable {
int startValue;
int endValue;
int result;
public SumTask (int startValue, int endValue){
this.startValue = startValue;
this.endValue = endValue;
}
@Override
public void run() {
MyLogger.log("작업 시작");
ThreadUtils.sleep(2000);
for (int i = startValue; i <= endValue; i++) {
result += i;
}
MyLogger.log("작업 종료, resule = " + result);
}
}
}