-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadYieldTest.java
More file actions
53 lines (44 loc) · 1.04 KB
/
ThreadYieldTest.java
File metadata and controls
53 lines (44 loc) · 1.04 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
package thread;
class MyThread4 implements Runnable{
boolean flag;
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
if(flag) {
Thread.yield();
} else {
System.out.println(Thread.currentThread().getName()+"실행");
for ( int i = 0 ; i <= 100000000 ; i ++) {
}
}
}
}
}
public class ThreadYieldTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread4 obj = new MyThread4();
Thread t1 = new Thread (obj);
t1.setName("t1");
obj.flag = false;
t1.setDaemon(true);//데몬쓰레드로 변경(메인쓰레드가 종료되면 같이 종료되는 쓰레드)
t1.start();
MyThread4 obj2 = new MyThread4();
Thread t2 = new Thread(obj2);
t2.setName("t2");
obj2.flag = false;
t2.setDaemon(true);
t2.start();
for ( int i = 1; i<=6; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
// TODO: handle exception
}
obj.flag = !obj.flag;
obj2.flag = !obj2.flag;
}
}
}