【java多线程】【生产者与消费者】【二】【多对多】

来源:互联网 发布:梦里花落知多少对应句 编辑:程序博客网 时间:2024/05/20 03:43

本文是生产者与消费者的实例,多个生产者,多个消费者。

1、2线程为生产者,3、4线程为消费者

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;/** * Created by patkritLee on 2017/1/5. */public class ProducerConsumerDemo {    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 cnt = 1;    private boolean flag = false;    public synchronized void set(String name){        while(flag){            try{                this.wait();            }catch (Exception e){}        }        this.name = name+"--"+cnt++;        System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);        flag = true;        this.notifyAll();    }    public synchronized void out(){        while(!flag){            try{                wait();            }catch (Exception e){}        }        System.out.println(Thread.currentThread().getName()+"...消费者......"+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();        }    }}
部分执行结果:

Thread-0...生产者...+商品+--77074Thread-3...消费者......+商品+--77074Thread-1...生产者...+商品+--77075Thread-2...消费者......+商品+--77075Thread-0...生产者...+商品+--77076Thread-3...消费者......+商品+--77076Thread-1...生产者...+商品+--77077Thread-2...消费者......+商品+--77077Thread-1...生产者...+商品+--77078Thread-2...消费者......+商品+--77078Thread-1...生产者...+商品+--77079Thread-2...消费者......+商品+--77079Thread-1...生产者...+商品+--77080Thread-2...消费者......+商品+--77080Thread-0...生产者...+商品+--77081Thread-2...消费者......+商品+--77081Thread-1...生产者...+商品+--77082Thread-3...消费者......+商品+--77082Thread-1...生产者...+商品+--77083Thread-2...消费者......+商品+--77083Thread-0...生产者...+商品+--77084Thread-2...消费者......+商品+--77084Thread-1...生产者...+商品+--77085Thread-3...消费者......+商品+--77085Thread-1...生产者...+商品+--77086Thread-2...消费者......+商品+--77086Thread-0...生产者...+商品+--77087Thread-2...消费者......+商品+--77087Thread-1...生产者...+商品+--77088Thread-3...消费者......+商品+--77088Thread-1...生产者...+商品+--77089Thread-2...消费者......+商品+--77089Thread-0...生产者...+商品+--77090Thread-2...消费者......+商品+--77090Thread-1...生产者...+商品+--77091Thread-3...消费者......+商品+--77091Thread-1...生产者...+商品+--77092Thread-2...消费者......+商品+--77092Thread-0...生产者...+商品+--77093Thread-2...消费者......+商品+--77093Thread-1...生产者...+商品+--77094Thread-3...消费者......+商品+--77094Thread-1...生产者...+商品+--77095Thread-2...消费者......+商品+--77095Thread-0...生产者...+商品+--77096Thread-2...消费者......+商品+--77096Thread-1...生产者...+商品+--77097Thread-3...消费者......+商品+--77097Thread-1...生产者...+商品+--77098Thread-2...消费者......+商品+--77098Thread-0...生产者...+商品+--77099Thread-2...消费者......+商品+--77099Thread-1...生产者...+商品+--77100Thread-3...消费者......+商品+--77100Thread-1...生产者...+商品+--77101Thread-2...消费者......+商品+--77101Thread-0...生产者...+商品+--77102Thread-2...消费者......+商品+--77102Thread-1...生产者...+商品+--77103Thread-3...消费者......+商品+--77103Thread-1...生产者...+商品+--77104Thread-2...消费者......+商品+--77104Thread-0...生产者...+商品+--77105Thread-2...消费者......+商品+--77105Thread-1...生产者...+商品+--77106Thread-3...消费者......+商品+--77106Thread-1...生产者...+商品+--77107Thread-2...消费者......+商品+--77107Thread-0...生产者...+商品+--77108Thread-2...消费者......+商品+--77108



0 0
原创粉丝点击