-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadRound.java
More file actions
43 lines (30 loc) · 874 Bytes
/
ThreadRound.java
File metadata and controls
43 lines (30 loc) · 874 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
package com.leetcode.thread;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class ThreadRound {
private final static Unsafe UNSAFE;
static {
try {
Class<?> clazz=Class.forName("sun.misc.Unsafe");
Field field=clazz.getDeclaredField("theUnsafe");
field.setAccessible(true);
UNSAFE=(Unsafe) field.get(null);
} catch (Exception e) {
throw new Error();
}
}
public void main(Thread thread) {
for(int i=1;i<=50;i++){
System.out.println("主线程:"+i);
}
UNSAFE.unpark(thread);
UNSAFE.park(false,0L);
}
public void sub(Thread thread) {
UNSAFE.park(false,0l);
for(int i=1;i<=10;i++){
System.out.println("子线程:"+i);
}
UNSAFE.unpark(thread);
}
}