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

© noiary 高级黑马   /  2014-9-13 23:55  /  1148 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

多线程略难,时间也少. 第十二天,两天了还没吃透.

不过我想,多线程应该是java中的重点,学好这里下面才会相对轻松些...

很期待13天的String ~


  1. /*
  2. 生产者消费者练习
  3. synchronized版

  4. */

  5. public class ProducerConsumerDemo {

  6.         public static void main(String[] args) {
  7.        
  8.                 Resource res = new Resource();
  9.                
  10.                 new Thread(new Producer(res)).start();
  11.                 new Thread(new Producer(res)).start();
  12.                 new Thread(new Consumer(res)).start();
  13.                 new Thread(new Consumer(res)).start();
  14.         }
  15. }

  16. class Resource {
  17.        
  18.         private String name;
  19.         private int count = 1;
  20.         private boolean flag;

  21.         public synchronized void set(String name) {
  22.        
  23.                 while(flag)
  24.                         try{wait();} catch(Exception e) {}
  25.                 this.name = name + "--" + count++;
  26.                 System.out.println(Thread.currentThread().getName() + "\t生产者" + this.name);
  27.                 flag = true;
  28.                 notifyAll();
  29.         }
  30.        
  31.         public synchronized void out() {
  32.        
  33.                 while(!flag)
  34.                         try{wait();} catch(Exception e) {}
  35.                 System.out.println(Thread.currentThread().getName() + "\t\t消费者" + this.name);
  36.                 flag = false;
  37.                 notifyAll();
  38.         }
  39. }


  40. class Producer implements Runnable {

  41.         Resource res;
  42.        
  43.         public Producer(Resource res) {
  44.        
  45.                 this.res = res;
  46.         }
  47.        
  48.         public void run() {
  49.        
  50.                 while(true) {
  51.                
  52.                         res.set("商品");
  53.                 }
  54.         }
  55. }

  56. class Consumer implements Runnable {

  57.         Resource res;
  58.        
  59.         public Consumer(Resource res) {
  60.        
  61.                 this.res = res;
  62.         }
  63.        
  64.         public void run() {
  65.        
  66.                 while(true) {
  67.                
  68.                         try{Thread.sleep(500);} catch(Exception e) {}
  69.                         res.out();
  70.                 }
  71.         }
  72. }
复制代码


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


  2. /*
  3. 生产者消费者练习
  4. ReentrantLock版

  5. */
  6. public class ProducerConsumerDemo2 {

  7.         public static void main(String[] args) {
  8.        
  9.                 Resource res = new Resource();
  10.                
  11.                 new Thread(new Producer(res)).start();
  12.                 new Thread(new Producer(res)).start();
  13.                 new Thread(new Consumer(res)).start();
  14.                 new Thread(new Consumer(res)).start();
  15.         }
  16. }

  17. class Resource {

  18.         private String name;
  19.         private int count = 1;
  20.         private boolean flag;
  21.        
  22.         private Lock lock = new ReentrantLock();
  23.         private Condition condition_pro = lock.newCondition();
  24.         private Condition condition_con = lock.newCondition();
  25.        
  26.         public void set(String name)
  27.                 throws InterruptedException {
  28.        
  29.                 lock.lock();
  30.                 try {
  31.                
  32.                         if(flag)
  33.                                 condition_pro.await();
  34.                         this.name = name + "--" + count++;
  35.                         System.out.println(Thread.currentThread().getName() + "生产者" + this.name);
  36.                         flag = true;
  37.                         condition_con.signal();
  38.                 }
  39.                 finally {
  40.                
  41.                         lock.unlock();
  42.                 }
  43.         }
  44.        
  45.         public void out()
  46.                 throws InterruptedException {
  47.        
  48.                 lock.lock();
  49.                 try {
  50.                
  51.                         if(!flag)
  52.                                 condition_con.await();
  53.                         System.out.println(Thread.currentThread().getName() + "\t\t消费者" + this.name);
  54.                         flag = false;
  55.                         condition_pro.signal();
  56.                 }
  57.                 finally {
  58.                
  59.                         lock.unlock();
  60.                 }
  61.         }
  62. }



  63. class Producer implements Runnable {

  64.         Resource res;
  65.        
  66.         public Producer(Resource res) {
  67.        
  68.                 this.res = res;
  69.         }
  70.        
  71.         public void run() {
  72.        
  73.                 while(true) {
  74.                
  75.                         try {
  76.                                
  77.                                 res.set("商品");
  78.                         }
  79.                         catch(InterruptedException e) {
  80.                        
  81.                         }
  82.                 }
  83.         }
  84. }

  85. class Consumer implements Runnable {

  86.         Resource res;
  87.        
  88.         public Consumer(Resource res) {
  89.        
  90.                 this.res = res;
  91.         }
  92.        
  93.         public void run() {
  94.        
  95.                 while(true) {
  96.                
  97.                         try {
  98.                        
  99.                                 res.out();
  100.                         }
  101.                         catch(InterruptedException e) {
  102.                        
  103.                         }
  104.                 }
  105.         }
  106. }
复制代码

6 个回复

倒序浏览
如果能把张老师的Java并发库视频看完,并用里面的每种工具把生产者消费者问题实现一遍,你的Java多线程就算入门了
回复 使用道具 举报
感谢分享。
回复 使用道具 举报
fantacyleo 发表于 2014-9-14 01:12
如果能把张老师的Java并发库视频看完,并用里面的每种工具把生产者消费者问题实现一遍,你的Java多线程就算 ...

好的,一定实践~!  
回复 使用道具 举报
并发库的视频在哪里啊
回复 使用道具 举报
不系之舟王 来自手机 中级黑马 2014-9-15 00:24:44
地板
学习一下
回复 使用道具 举报
学习学习
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马