线程1打印:
1-52
线程2打印:
A-Z
public class Main {
public static void main(String args[]){
String s = "";
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
int n = 1;
synchronized (s) {
while (n <= 52) {
s.notifyAll();
System.out.print(n++);
System.out.print(n++);
try {
s.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
char tag ='A';
synchronized (s) {
while (tag <= 'Z') {
s.notifyAll();
System.out.print(tag++);
System.out.print(" ");
try {
s.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
thread1.start();
thread2.start();
}
}
运行截图