黑马程序员技术交流社区

标题: 毕老师视频里生产者消费者问题 [打印本页]

作者: 汤密奕    时间: 2012-7-9 00:56
标题: 毕老师视频里生产者消费者问题
本帖最后由 汤密奕 于 2012-7-10 23:19 编辑

class Resource
{
        private String name;
        private int conut;
        private boolean flag = false;
        public synchronized void set(String name)
        {

                try {
                        Thread.sleep(500);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                while(flag)
                {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                this.name = name+conut++;
                System.out.println(Thread.currentThread().getName()+":生产了"+this.name);
                flag = true;
                this.notifyAll();
        }
        
        public synchronized void out()
        {
                try {
                        Thread.sleep(500);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                while(!flag)
                {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                System.out.println(Thread.currentThread().getName()+":消费了"+this.name);
                flag = false;
                this.notifyAll();
        }
        
}

class Producer implements Runnable
{
        private Resource r;
        Producer(Resource r)
        {
                this.r = r;
        }
        public void run()
        {
                while(true)
                {
                        r.set("商品");
                }
        }
}

class Consumer implements Runnable
{
        private Resource r;
        Consumer(Resource r)
        {
                this.r = r;
        }
        public void run()
        {
                while(true)
                        r.out();
        }
}


public class ProducerConsumer {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Resource r = new Resource();
                Producer p = new Producer(r);
                Consumer c = new Consumer(r);
               
                Thread t1 = new Thread(p);
                Thread t2 = new Thread(p);
                Thread t3 = new Thread(c);
                Thread t4 = new Thread(c);
               
                t1.start();
                t2.start();
                t3.start();
                t4.start();
               

        }

}

现在我想当生产完第10个商品并将其消费掉,即当控制台打印出“Thread-x:消费了商品10”,就让整个程序终止,要怎么弄?
作者: 杨_扬    时间: 2012-7-9 00:58
你的问题提的太早了,继续看视频,生产者,消费者下面就讲如何停止线程了
作者: 游兴钟    时间: 2012-7-9 12:51
class Resource
{
        private String name;
        private int conut;
        private boolean flag = false;
        public synchronized void set(String name)
        {

                try {
                        Thread.sleep(500);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                while(flag)
                {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                this.name = name+conut++;
                System.out.println(Thread.currentThread().getName()+":生产了"+this.name);
                flag = true;
                this.notifyAll();
        }
        
        public synchronized void out()
        {
                try {
                        Thread.sleep(500);
                } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                while(!flag)
                {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                               
                System.out.println(Thread.currentThread().getName()+":消费了"+this.name);
        if(this.name.equals("商品10"))
        System.exit(0);
//只要在这里加上红色的两句语句就可以了               
                flag = false;
                this.notifyAll();
        }
        
}

class Producer implements Runnable
{
        private Resource r;
        Producer(Resource r)
        {
                this.r = r;
        }
        public void run()
        {
                while(true)
                {
                        r.set("商品");
                }
        }
}

class Consumer implements Runnable
{
        private Resource r;
        Consumer(Resource r)
        {
                this.r = r;
        }
        public void run()
        {
                while(true)
                        r.out();
        }
}


public class ProducerConsumer {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Resource r = new Resource();
                Producer p = new Producer(r);
                Consumer c = new Consumer(r);
               
                Thread t1 = new Thread(p);
                Thread t2 = new Thread(p);
                Thread t3 = new Thread(c);
                Thread t4 = new Thread(c);
               
                t1.start();
                t2.start();
                t3.start();
                t4.start();
               

        }

}


作者: 汤密奕    时间: 2012-7-9 13:04
achilles 发表于 2012-7-9 12:51
class Resource
{
        private String name;

呵呵,谢谢,一直想着怎么停止线程,糊涂了。。。
作者: 陈虹旭    时间: 2012-7-9 17:32
我晕  看不懂啊  都这么强悍!
作者: 游兴钟    时间: 2012-7-10 15:07
汤密奕 发表于 2012-7-9 13:04
呵呵,谢谢,一直想着怎么停止线程,糊涂了。。。

呵呵 学习过程遇到问题很正常 互相帮助而已




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