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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© outsider1020 中级黑马   /  2014-12-23 10:40  /  917 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

生产者与消费者:
(1)使用同步来保证一个操作的完整性
(2)Object类中的wait方法表示让当前线程进入等待池,让出CPU时间,并且释放该对象上的监视器所属权(对象锁)
(3)Object类中的notify()/notifyAll()方法表示唤醒当前对象上等待池中的一个/所有线程
(4)sleep 和 wait 的区别:
        sleep:让当前线程休眠,让出CPU时间,不释放对象锁
        wait:让当前线程进入等待池,让出CPU时间,释放对象锁。
//面包类
public class Bread {
         private String name ;
         boolean flag =true ;//表示此时可以生产,不可以消费。
         public String getName() {
                 return name ;
         }
         public void setName( String name) {
                 this. name = name;
         }
         //生产面包的方法
         public synchronized void setBread(String name){
                 if(! flag){//已经生产好,等待被消费
                         try {
                                 this. wait();
                         } catch (InterruptedException e) {
                                e .printStackTrace() ;
                         }
                 }
                 try {
                        Thread .sleep( 400);
                 } catch (InterruptedException e) {
                        e .printStackTrace() ;
                 }
                 setName(name );
                System .out. println("生产了" +getName()) ;
                 flag=false; //提示已经生产好,需要被消费
                 this. notify();
               
         }
         //消费面包的方法
         public synchronized void getBread(){
                 if(flag ){//正在生产,不能消费
                         try {
                                 this. wait();
                         } catch (InterruptedException e) {
                                e .printStackTrace() ;
                         }
                 }
                 System.out. println(Thread .currentThread() .getName() +"消费了"+getName ());
                 flag=true; //提示已经消费完,需要生产
                 this. notify();
         }
}
//生产者
public class Product implements Runnable {
         private Bread bread ;
//     private boolean flag=true;
         public Product (Bread bread ){
                 this. bread=bread ;
         }
        
         public void run() {
                 //生产面包
                 for (int i = 1; i <=10; i++) {
                                 bread.setBread ("面包"+ i);
                 }
         }

}
//消费者
public class Consumer implements Runnable {
         private Bread bread ;
         public Consumer (Bread bread ){
                 this. bread=bread ;
         }
         public void run() {
                 for(int i= 1;i <= 10; i++){
                 try {
                        Thread .sleep( 100);
                 } catch (InterruptedException e) {
                        e .printStackTrace() ;
                 }
                 bread.getBread ();
                 }
         }

}
public class Test {
         public static void main(String [] args ) {
                Bread b=new Bread ();
                Product p=new Product (b);
                Consumer c1=new Consumer (b);
                Consumer c2=new Consumer (b);
                Thread pt=new Thread (p);
                Thread ct1=new Thread (c1);
                Thread ct2=new Thread (c2);
                ct1 .setName( "A");
                ct2 .setName( "B");
                pt .start() ;//启动生产者线程
                ct1 .start() ;//启动消费者线程
                ct2 .start() ;//启动消费者线程
         }

}

2 个回复

倒序浏览
总结的很好,看看学学
回复 使用道具 举报
权当笔记学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马