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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangqing_tt 中级黑马   /  2015-4-13 09:41  /  984 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天做了一个毕老师教的java多线程的练习题,代码如下:
import java.util.concurrent.locks.*;

class ProducerConsumerDemo2
{
        public static void main(String[] args)
        {
                Resource r = new Resource();

                Producer pro = new Producer(r);
                Consumer con = new Consumer(r);

                Thread t1 = new Thread(pro);
                Thread t2 = new Thread(pro);
                Thread t3 = new Thread(con);
                Thread t4 = new Thread(con);

                t1.start();
                t2.start();
                t3.start();
                t4.start();

        }
}
class Resource
{
        private String name;
        private int count = 1;
        private boolean flag = false;
                        //  t1    t2
        private Lock lock = new ReentrantLock();

        private Condition condition_pro = lock.newCondition();
        private Condition condition_con = lock.newCondition();



        public  void set(String name)throws InterruptedException
        {
                lock.lock();
                try
                {
                        while(flag)
                                condition_pro.await();//t1,t2
                        this.name = name+"--"+count++;

                        System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
                        flag = true;
                        condition_con.signal();
                }
                finally
                {
                        lock.unlock();//释放锁的动作一定要执行。
                }
        }


        //  t3   t4  
        public  void out()throws InterruptedException
        {
                lock.lock();
                try
                {
                        while(!flag)
                                condition_con.await();
                        System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
                        flag = false;
                        condition_pro.signal();
                }
                finally
                {
                        lock.unlock();
                }
               
        }
}

class Producer implements Runnable
{
        private Resource res;

        Producer(Resource res)
        {
                this.res = res;
        }
        public void run()
        {
                while(true)
                {
                        try
                        {
                                res.set("+商品+");
                        }
                        catch (InterruptedException e)
                        {
                        }
                       
                }
        }
}

class Consumer implements Runnable
{
        private Resource res;

        Consumer(Resource res)
        {
                this.res = res;
        }
        public void run()
        {
                while(true)
                {
                        try
                        {
                                res.out();
                        }
                        catch (InterruptedException e)
                        {
                        }
                }
        }
}
该怎么写控制条件,使得Resource到100个线程就结束呢

10 个回复

倒序浏览
用一个计数器,在启动线程的方法里计数,变量可以采用局部变量(一口气把线程都创建执行),或者带锁的静态变量
回复 使用道具 举报
可不可以给我写一下计数器的代码,本人菜鸟一枚?万分感谢啊
回复 使用道具 举报
在你while中加个if循环,看一下行不行。
try
                {
                        while(flag)
                               if(int i=0;i<100;i++)
                              {
                                    condition_pro.await();//t1,t2
                                    this.name = name+"--"+count++;

                                     System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
                               }
                        flag = true;
                        condition_con.signal();
                }
回复 使用道具 举报
学习了学习了。
回复 使用道具 举报
学习了学习了。
回复 使用道具 举报
cody 中级黑马 2015-4-13 17:33:03
7#
路过,表示还没学到多线程
回复 使用道具 举报
表示还没学到这儿啊!:(
回复 使用道具 举报
mah707 中级黑马 2015-4-13 18:28:26
9#
正是我要找的多线程代码,我没看视频,看书呢,呵呵
回复 使用道具 举报
有难度
回复 使用道具 举报
没注释看起来真费劲……
需求也没写出来……
想要控制的话直接设置个静态变量不就行了么。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马