写两个线程,一个线程打印 1-52,另一个线程打印A~Z,打印顺序是12A34B…5152Z;需要用到线程通信我们用flag表示
class Print{
private int flag = 1; // 线程通信标记
private int count = 1;
// 打印数字线程
public synchronized void printNum(){
while(flag!=1){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(2*count-1); // 标记为1打印数字
System.out.print(2*count);
flag = 2;
notify();
}
// 打印字母线程
public synchronized void printChar(){
while(flag!=2){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print((char )(count-1+'A')); // 标记为2打印字母
flag = 1;
count++;
notify();
}
}
public class Tes