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

本帖最后由 茄子 于 2014-6-9 23:15 编辑

马儿们,茄子又来献丑了,今天是生产者与消费者同步问题发生全部线程等待的情况,

自己看了半天,还是没有找出原因,这是为什么呢??

下面是代码:

class Resource
{
        private String name;
        private int count=1;
        private boolean flag=false;
        public synchronized void set(String name)
        {
                while(flag)
                                try{this.wait();}catch(Exception e){}
                        this.name=name+count++;
                        System.out.println(Thread.currentThread().getName()+name+"……生产者……"+this.name);
                        flag=true;
                        this.notifyAll();
               
        }
        public synchronized void out()
        {
                while(!flag)
                                try{this.wait();}catch(Exception e){}
                        System.out.println(Thread.currentThread().getName()+name+"……………消费者……………"+this.name);
                        flag=false;
                        this.notifyAll();
               
        }

}
class Producer implements Runnable
{
        private Resource res;
        Producer(Resource res)
        {
                this.res=res;
        }
        public void run()
        {
                res.set("商品");
        }
}
class Consumer implements Runnable
{
        private Resource res;
        Consumer(Resource res)                  //自定义构造函数
        {
                this.res=res;
        }
        public void run()
        {
                res.out();
        }
}

public class ProducerConsumerDemo {

        public static void main(String[] args) {
                Resource res=new Resource();
               
                Producer p=new Producer(res);               
                Consumer c=new Consumer(res);
               
               
                Thread t1=new Thread(p);                  //2个线程同时生产,
                Thread t2=new Thread(p);
                Thread t3=new Thread(c);                  //2个线程负责消费,
                Thread t4=new Thread(c);
               
                t1.start();
                t2.start();
                t3.start();
                t4.start();
        }

}



运行结果是:
Thread-0商品……生产者……商品1
Thread-3商品1……………消费者……………商品1
Thread-1商品……生产者……商品2
Thread-2商品2……………消费者……………商品2




这是为什么呢????


评分

参与人数 1技术分 +1 收起 理由
李小然 + 1 茄子兄加油~

查看全部评分

4 个回复

倒序浏览
你只是运行一次,所以只打印4条,你要测多线程,应该在run()方法中加入循环
回复 使用道具 举报
package org.test;

class Resource {
        private String name;
        private int count = 1;
        private boolean flag = false;

        public synchronized void set(String name) {
                while (flag)
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }
                this.name = name + count++;
                System.out.println(Thread.currentThread().getName() + name + "……生产者……"
                                + this.name);
                flag = true;
                this.notifyAll();

        }

        public synchronized void out() {
                while (!flag)
                        try {
                                this.wait();
                        } catch (Exception e) {
                        }
                System.out.println(Thread.currentThread().getName() + name
                                + "……………消费者……………" + this.name);
                flag = false;
                this.notifyAll();

        }

}

class Producer implements Runnable {
        private Resource res;

        Producer(Resource res) {
                this.res = res;
        }

        public void run() {
                while(true)
                res.set("商品");
        }
}

class Consumer implements Runnable {
        private Resource res;

        Consumer(Resource res) // 自定义构造函数
        {
                this.res = res;
        }

        public void run() {
                while(true)
                res.out();
        }
}

public class ProducerConsumerDemo {

        public static void main(String[] args) {
                Resource res = new Resource();

                Producer p = new Producer(res);
                Consumer c = new Consumer(res);

                Thread t1 = new Thread(p); // 2个线程同时生产,
                Thread t2 = new Thread(p);
                Thread t3 = new Thread(c); // 2个线程负责消费,
                Thread t4 = new Thread(c);

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

}





Thread-0商品……生产者……商品52607
Thread-3商品52607……………消费者……………商品52607
Thread-1商品……生产者……商品52608
Thread-2商品52608……………消费者……………商品52608
Thread-0商品……生产者……商品52609
Thread-3商品52609……………消费者……………商品52609
Thread-1商品……生产者……商品52610
Thread-2商品52610……………消费者……………商品52610
Thread-0商品……生产者……商品52611
Thread-3商品52611……………消费者……………商品52611
Thread-1商品……生产者……商品52612
Thread-2商品52612……………消费者……………商品52612
Thread-0商品……生产者……商品52613
Thread-3商品52613……………消费者……………商品52613
Thread-1商品……生产者……商品52614
Thread-2商品52614……………消费者……………商品52614
Thread-0商品……生产者……商品52615
Thread-3商品52615……………消费者……………商品52615
Thread-1商品……生产者……商品52616
Thread-2商品52616……………消费者……………商品52616
Thread-0商品……生产者……商品52617
Thread-3商品52617……………消费者……………商品52617
Thread-1商品……生产者……商品52618
Thread-2商品52618……………消费者……………商品52618
Thread-0商品……生产者……商品52619
Thread-3商品52619……………消费者……………商品52619
Thread-1商品……生产者……商品52620
Thread-2商品52620……………消费者……………商品52620

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

回复 使用道具 举报
轩辕冰晨 发表于 2014-6-9 23:02
你只是运行一次,所以只打印4条,你要测多线程,应该在run()方法中加入循环

谢谢兄弟!!!受教了!
回复 使用道具 举报
茄子 发表于 2014-6-9 23:14
谢谢兄弟!!!受教了!

哈,不用客气...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马