黑马程序员技术交流社区

标题: 怎么控制循环结束条件 [打印本页]

作者: yangqing_tt    时间: 2015-4-13 09:41
标题: 怎么控制循环结束条件
今天做了一个毕老师教的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个线程就结束呢

作者: sisel    时间: 2015-4-13 09:45
用一个计数器,在启动线程的方法里计数,变量可以采用局部变量(一口气把线程都创建执行),或者带锁的静态变量
作者: yangqing_tt    时间: 2015-4-13 09:54
可不可以给我写一下计数器的代码,本人菜鸟一枚?万分感谢啊
作者: 晓月清晖明    时间: 2015-4-13 12:40
在你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();
                }
作者: 百思不得解    时间: 2015-4-13 13:12
学习了学习了。
作者: 飞翔的侠客604    时间: 2015-4-13 15:32
学习了学习了。
作者: cody    时间: 2015-4-13 17:33
路过,表示还没学到多线程
作者: dgrlucky    时间: 2015-4-13 18:13
表示还没学到这儿啊!:(
作者: mah707    时间: 2015-4-13 18:28
正是我要找的多线程代码,我没看视频,看书呢,呵呵
作者: 冻了冬天    时间: 2015-4-13 18:47
有难度
作者: 君嘘    时间: 2015-4-13 19:38
没注释看起来真费劲……
需求也没写出来……
想要控制的话直接设置个静态变量不就行了么。




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