要求用线程顺序打印A1B2C3…Z26
wait notify实现
package com.mashibing.juc.c_026_00_interview.A1B2C3;
public class T01_00_Question {
public static void main(String[] args) {
Object lock = new Object();
new Thread(() -> {
synchronized (lock) {
char s = 'A';
try {
for (int i = 0; i < 26; i++) {
System.out.print(s++);
lock.notifyAll();
lock.wait();
}
lock.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
synchronized (lock) {
int s = 1;
try {
for (int i = 0; i < 26; i++) {
System.out.print(s++);
lock.notifyAll();
lock.wait();
}
lock.notifyAll();