A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© RockLee 中级黑马   /  2015-7-2 13:49  /  345 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class 线程的例子之四线程的通讯问题 {
  2.         public static void main(String[] args) {
  3.                 Person p = new Person();
  4.         Thread a = new Thread(new productor(p), "生产者");
  5.         Thread b = new Thread(new Consumer(p), "消费者");
  6.         a.start();
  7.         b.start();
  8.         }

  9. }

  10. class Person {
  11.         private String name;
  12.         private String  sex;
  13.         Boolean flag = false;
  14.          Person(String name,String sex){
  15.                 this.name = name;
  16.                 this.sex = sex;
  17.         }
  18.         public Person() {
  19.                 // TODO Auto-generated constructor stub
  20.         }
  21.         public void setNmae(String name){
  22.                 this.name = name;
  23.         }
  24.         public void setSex(String  sex){
  25.                 this.sex = sex;
  26.         }
  27.         public String getName(){
  28.                 return this.name;
  29.                
  30.         }
  31.         public String getSex(){
  32.                 return this.sex;
  33.                
  34.         }
  35.        
  36. }

  37. class productor implements Runnable {
  38.    int i;
  39.         Person p;
  40.        
  41.         productor(Person p){
  42.                 this.p = p;
  43.         }
  44.         @Override
  45.         public void run() {
  46.                 synchronized (p) {
  47.                         while(p.flag == true){
  48.                                 try {
  49.                                         p.wait();
  50.                                 } catch (InterruptedException e) {
  51.                                         // TODO Auto-generated catch block
  52.                                         e.printStackTrace();
  53.                                 }
  54.                         }
  55.                                   if(i%2 == 0){
  56.                                      p.setNmae("小红");
  57.                                      p.setSex("女");
  58.                                    
  59.                                      System.out.println(Thread.currentThread().getName()+"生产了"+p.getName()+p.getSex());}
  60.                      
  61.              
  62.                                         else
  63.                                         { p.setNmae("小明");
  64.                                  p.setSex("男");
  65.                                          System.out.println(Thread.currentThread().getName()+"生产了"+p.getName()+p.getSex());}
  66.                                   i++;
  67.                                p.flag  = true;
  68.                                p.notifyAll();
  69.                         }
  70.                
  71.                         }
  72.                 }
  73.    
  74.        
  75.        


  76. class Consumer implements Runnable{
  77.         Person p;
  78.         Consumer(Person p){
  79.                 this.p = p;
  80.         }
  81.         @Override
  82.         public void run() {
  83.                 // TODO Auto-generated method stub
  84.         synchronized (p) {
  85.                 while(p.flag == false){
  86.                 try {
  87.                         p.wait();
  88.                 } catch (InterruptedException e) {
  89.                         // TODO Auto-generated catch block
  90.                         e.printStackTrace();
  91.                 }
  92.                 }
  93.        
  94.                
  95.                
  96.                  System.out.println(Thread.currentThread().getName()+"消费了"+p.getName()+p.getSex());
  97.                  
  98.                  p.flag = false;
  99.                  p.notifyAll();
  100.        
  101.                
  102.         }       
  103.         }
  104.        
  105. }
复制代码

1 个回复

倒序浏览
:):):):):)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马