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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 池中月 中级黑马   /  2015-7-4 22:04  /  632 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

1、JDK5中提供多线程解决方案

  • 将同步synchronized替换成lock操作
  • 将object中的wait/notify/notifyALl替换成condtion对象
  • 该对象可以通过lock锁进行获取

示例:

  1. package unit18;

  2. import java.util.concurrent.locks.*;

  3. class Resources{
  4.         private String name;
  5.         private int count = 1;
  6.         private Boolean flag = false;
  7.        
  8.         private Lock lock = new ReentrantLock();//定义Lock类
  9.        
  10.         private Condition condition_pro = lock.newCondition();
  11.         private Condition condition_con = lock.newCondition();
  12.        
  13.         public  void set(String name)throws InterruptedException{
  14.                 lock.lock();//添加锁
  15.                 try{
  16.                 while(flag){
  17.                         condition_pro.wait();//生产者调用线程等待
  18.                   }
  19.                 this.name = name+"__ " + count++;
  20.                 System.out.println(Thread.currentThread().getName()+ "......生产者" + this.name);
  21.                 flag = true;
  22.                 condition_con.signal();        //唤醒消费者线程
  23.                 }catch(Exception e){}
  24.                 finally{
  25.                         lock.unlock();//解锁
  26.                 }
  27.         }
  28.        
  29.         public  void show()throws InterruptedException{
  30.                 lock.lock();
  31.                 try{
  32.                 while(!flag){
  33.                         condition_con.wait();
  34.                 }
  35.                
  36.                 System.out.println(Thread.currentThread().getName()+"消费者"+ this.name);
  37.                 flag = false;
  38.                 condition_pro.signal();//唤醒生产者线程
  39.                 }catch(Exception e){}
  40.                 finally{
  41.                         lock.unlock();
  42.                 }
  43.         }
  44. }


  45. class Producters implements Runnable{
  46.         private Resources r;
  47.         Producters(Resources r){
  48.                 this.r = r;
  49.         }
  50.         public void run(){
  51.                 while(true){
  52.                         try {
  53.                                 r.set("商品");
  54.                         } catch (InterruptedException e) {
  55.                        
  56.                         }
  57.                 }
  58.         }
  59. }

  60. class Customers implements Runnable{
  61.         private Resources r;
  62.         Customers(Resources r){
  63.                 this.r = r;
  64.         }
  65.         public void run(){
  66.                 while(true){
  67.                         try {
  68.                                 r.show();
  69.                         } catch (InterruptedException e) {
  70.                        
  71.                         }
  72.                 }
  73.         }
  74. }




  75. public class ProducterCustomerTest2 {

  76.         public static void main(String[] args) {
  77.                 Resources r = new Resources();
  78.                 Producters pro = new Producters(r);
  79.                 Customers cus = new Customers(r);
  80.                 Thread t1 = new Thread(pro);
  81.                 Thread t2 = new Thread(pro);
  82.                 Thread t3 = new Thread(cus);
  83.                 Thread t4 = new Thread(cus);
  84.                 t1.start();
  85.                 t2.start();
  86.                 t3.start();
  87.                 t4.start();
  88.         }


  89. }
复制代码


1 个回复

倒序浏览
分析的很认真,学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马