利用线程的通信机制,用两个线程打印以下的结果: 1 2 A 3 4 B 5 6 C 7 8 D ... 49 50 Y 51 52 Z 5

 我来答
百度网友ce0535a
2017-03-06
知道答主
回答量:8
采纳率:0%
帮助的人:8593
展开全部
package com.myThread;
public class TestMain {
     public static Object object = new Object();//共享资源
     public static boolean flag = true;//判断是否线程要结束
     public static void main(String[] args) {
          Thread t1 = new Thread(new ThreadNumber(object));
          Thread t2 = new Thread(new ThreadChar(object));
          t1.start();
          t2.start();
     }
}
//打印数字线程
class ThreadNumber implements Runnable {
     private Object object;
     public ThreadNumber(Object object) {
          this.object = object;
     }
     @Override
     public void run() {
          while(TestMain.flag){
              synchronized (object) {//共享资源上锁
                   for (int i = 1; i < 110; i++) {
                        //如果别的线程结束了,自己也跟着结束
                        if (!TestMain.flag) {
                             break;
                        }
                        System.out.print(i);
                        if (i % 2 == 0) {
                             //唤醒其他线程打印
                             object.notifyAll();
                             try {
                                  //本身进入等待
                                  object.wait();
                             } catch (InterruptedException e) {
                                  e.printStackTrace();
                             }
                        }
                   }
                   //自己打印完了,就设置结束标志为false,通知其他线程也结束
                   TestMain.flag = false;
                   //唤醒其他线程,让其他线程有机会继续,而不会一直处于等待
                   object.notifyAll();
              }
          }
     }
}
//打印字母线程
class ThreadChar implements Runnable {
     private Object object;
     public ThreadChar(Object object) {
          this.object = object;
     }
     
     @Override
     public void run() {
          String s[] = "A,B,C".split(",");
          while(TestMain.flag){
              synchronized (object) {//共享资源上锁
                   for(String string : s){
                        //如果别的线程结束了,自己也跟着结束
                        if (!TestMain.flag) {
                             break;
                        }
                        System.out.print(string);
                        object.notifyAll();
                        try {
                             //本身进入等待
                             object.wait();
                        } catch (InterruptedException e) {
                             e.printStackTrace();
                        }
                   }
                   //自己打印完了,就设置结束标志为false,通知其他线程也结束
                   TestMain.flag = false;
                   //唤醒其他线程,让其他线程有机会继续,而不会一直处于等待
                   object.notifyAll();
              }
          }
     }
}

亲测有效  

super_成仔
2013-08-07
知道答主
回答量:1
采纳率:0%
帮助的人:1477
展开全部
package com.work7;

public class Communication {
public static void main(String[] args) {
Object obj = new Object();

Thread t1 = new NumberThread2(obj);
Thread t2 = new StringThread2(obj);

t1.start();
t2.start();

}
}

class NumberThread2 extends Thread {
private Object obj;

public NumberThread2(Object obj) {
super();
this.obj = obj;
}

public void run() {
synchronized (obj) {
for (int i = 1; i <= 26; i++) {
System.out.print(i*2-1 + "\t");
System.out.print(i*2 + "\t");
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

class StringThread2 extends Thread {
private Object obj;

public StringThread2(Object obj) {
super();
this.obj = obj;
}

public void run() {
synchronized(obj){
for (char ch = 'A'; ch <= 'Z'; ch++) {
System.out.print(ch + "\t");
obj.notify();
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-08-07
展开全部

public class MyTestThread1 {
 public static void main(String[] args) {
  Object obj = new Object();
  Thread1 t1 = new Thread1(obj);
  t1.start();
  Thread2 t2 = new Thread2(obj);
  Thread thread = new Thread(t2);
  thread.start();
 }

}


class Thread1 extends Thread{
 private Object obj;

 public Thread1(Object obj) {
  this.obj = obj;
 }


 @Override
 public void run() {
  synchronized (obj) {
  for(int i = 1;i <= 26;i++){
   System.out.println(2*i - 1);
   System.out.println(2*i);
  }
  }
 }
}
class Thread2 implements Runnable{
 private Object obj;

 public Thread2(Object obj) {
  this.obj = obj;
 }

 public void run() {
  synchronized (obj) {
  for(char c ='A';c <='Z';c++){
   System.out.println(c);
  }
  }
 }
 
}

 
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
王1428397011
2012-12-03 · TA获得超过442个赞
知道小有建树答主
回答量:597
采纳率:22%
帮助的人:117万
展开全部
1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式