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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© binghaiwang 中级黑马   /  2013-7-23 22:59  /  1088 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 binghaiwang 于 2013-7-24 11:06 编辑
  1. import java.util.concurrent.locks.*;

  2. class  ThreadTest
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Goods g = new Goods();
  7.                 g.set("商品");
  8.                 g.out();
  9.         }
  10. }
  11. class Goods
  12. {
  13.         private String name;
  14.         private int count = 1;
  15.         private boolean flag = false;
  16.         private Lock lock = new ReentrantLock();
  17.         private Condition con_p = lock.newCondition();
  18.         private Condition con_c = lock.newCondition();

  19.         public void set(final String name)
  20.                
  21.         {
  22.                
  23.                 Runnable r = new Runnable()
  24.                 {
  25.                         public void run()
  26.                         {        
  27.                                 while (true)
  28.                                 {
  29.                                         lock.lock();
  30.                                         try
  31.                                         {
  32.                                                 while(flag)
  33.                                                         con_c.await();
  34.                                                 Goods.this.name = name+"--"+count++;
  35.                                                 System.out.println(Thread.currentThread().getName()+"..生产者.."+Goods.this.name);
  36.                                                 flag = true;
  37.                                                 con_p.signal();
  38.                                         }
  39.                                         catch (InterruptedException e)
  40.                                         {
  41.                                         }
  42.                                 
  43.                                         finally
  44.                                         {
  45.                                                 lock.unlock();
  46.                                         }
  47.                                 }
  48.                         }
  49.                 };
  50.                 new Thread(r).start();
  51.         }
  52.         public void out()
  53.         {
  54.                
  55.                 Runnable r = new Runnable()
  56.                 {
  57.                         public void run()
  58.                         {
  59.                                 while (true)
  60.                             {
  61.                                         lock.lock();
  62.                                         try
  63.                                         {
  64.                                                 while(!flag)
  65.                                                         con_p.await();
  66.                                                 
  67.                                                 System.out.println(Thread.currentThread().getName()+"..........消费者.."+Goods.this.name);
  68.                                                 flag = false;
  69.                                                 con_c.signal();
  70.                                         }
  71.                                         catch (InterruptedException e)
  72.                                         {
  73.                                         }
  74.                                        
  75.                                         finally
  76.                                         {
  77.                                                 lock.unlock();
  78.                                         }
  79.                                 }
  80.                         }
  81.                 };
  82.                 new Thread(r).start();
  83.                 new Thread(r).start();/*此处是自己试着看写上的,运行发现和普通写法{Thread t1 = new Thread(r)  Thread t2 = new Thread(r)  ,然后再各自start() }的 效果一样。 这里这样写是否正确,为什么参数是相同对象会建立不同的线程?*/
  84.         }        
  85. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

4 个回复

倒序浏览
new 了一个Thread就是创建了一个线程,给他传递参数是给线程分配任务,也就是说你是创建了两个线程,而他们执行的任务相同罢了
回复 使用道具 举报
这样写是正确的。new Thread()是创建了一个线程,你new了两个是两个不同的线程,里面传的相同的参数是让这两个线程都与这个对象联系起来,从而调用该对象的run方法。

评分

参与人数 1技术分 +1 收起 理由
特殊服务 + 1

查看全部评分

回复 使用道具 举报
你这边开启线程的条件是Thead类,你用了两个new字说明你开了两条线程.而那个参数是传递的是让你的Thread类启动的线程知道要运行的run()方法里面的代码在哪里.
回复 使用道具 举报
new Thread(r).start();
new Thread(r).start();
参数是相同但是放在了2个不同的对象中所以还是两个不同的线程,但可以运行
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马