//2. 写两个线程,一个线程打印1~52,另一个线程打印字母A~Z。打印顺序为12A34B56C........5152Z。要//求用线程间的通信
//主线程
public class Test2 {
public static void main(String[] args) {
MyThread3 thread3 =new MyThread3();
MyThread4 thread4 =new MyThread4();
Thread t3 =new Thread(thread3);
Thread t4 =new Thread(thread4);
t3.start();
t4.start();
}
}
//数字线程
public class MyThread3 implements Runnable{
@Override
public void run() {
synchronized (Object.class){
for (int i = 1; i <53 ; i++) {
System.out.print(i);
if(i%2==0){
Object.class.notifyAll();
try {
Object.class.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
//字母线程
public class MyThread4 implements Runnable {
@Override
public void run() {
synchronized (Object.class){
for (char i = 'A'; i <='Z' ; i++) {
System.out.print(i);
Object.class.notifyAll();
try {
Object.class.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
//打印字母
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交