黑马程序员技术交流社区

标题: 匿名内部类的多线程小疑问。 [打印本页]

作者: binghaiwang    时间: 2013-7-23 22:59
标题: 匿名内部类的多线程小疑问。
本帖最后由 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. }
复制代码

作者: 刘张朋    时间: 2013-7-23 23:18
new 了一个Thread就是创建了一个线程,给他传递参数是给线程分配任务,也就是说你是创建了两个线程,而他们执行的任务相同罢了
作者: 陈贺    时间: 2013-7-23 23:19
这样写是正确的。new Thread()是创建了一个线程,你new了两个是两个不同的线程,里面传的相同的参数是让这两个线程都与这个对象联系起来,从而调用该对象的run方法。

作者: 黄少海    时间: 2013-7-23 23:24
你这边开启线程的条件是Thead类,你用了两个new字说明你开了两条线程.而那个参数是传递的是让你的Thread类启动的线程知道要运行的run()方法里面的代码在哪里.
作者: yinjiek    时间: 2013-7-24 00:09
new Thread(r).start();
new Thread(r).start();
参数是相同但是放在了2个不同的对象中所以还是两个不同的线程,但可以运行




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2