编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为 12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数 后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束
public class Work4 {
public static void main(String[] args) {
Work4 w = new Work4();
new Thread(w::shuzi).start();
new Thread(w::zimu).start();
}
public synchronized void shuzi(){
for (int i = 1; i <= 52; i++) {
System.out.println("数字:" + i);
if (i % 2 == 0){
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public synchronized void zimu(){
for (char i = 'A';i <= 'Z';i++){
System.out.println("字母:" + i);
notify();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}