//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);
}
}
}
}
//打印字母
}
写两个线程,一个线程打印1~52,另一个线程打印字母A~Z。打印顺序为12A34B56C........5152Z。要求用线程间的通信
最新推荐文章于 2024-04-16 20:51:12 发布

3115

被折叠的 条评论
为什么被折叠?



