-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA1B2C3.java
43 lines (36 loc) · 1.01 KB
/
A1B2C3.java
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.algorithm.demo.thread;
import java.util.concurrent.locks.LockSupport;
/**
* 顺序打印 A 1 B 2 C 3 ... Z 26
*/
public class A1B2C3 {
static Thread t1 = null, t2 = null;
public static void main(String[] args) {
method();
}
public static void method(){
char[] a1 = "ABCDEFGHIGKLMNOPQRSTUVWXYZ".toCharArray();
t1 = new Thread(new Runnable() {
@Override
public void run() {
for(char s:a1){
System.out.println(s);
LockSupport.unpark(t2);
LockSupport.park();
}
}
}, "T1");
t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i =1 ; i < 27 ; i++){
LockSupport.park();
System.out.println(i);
LockSupport.unpark(t1);
}
}
}, "T2");
t1.start();
t2.start();
}
}