多线程打印输出为1a2b3c4d5e6f…26z
public class Main {
public static void main(String[] args) {
String lock = "锁子";
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 97; i < 123; i++) {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print((char)i);
lock.notify();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i < 27; i++) {
synchronized (lock) {
System.out.print(i);
lock.notify();
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
}