多线程生产者只有多个生产者,多个消费者!这里不讲基础的知识。java
代码以下多线程
package Thread; class Resource { private String name; private int count=0; private boolean flag=false; public synchronized void set(String name){ while (flag){ //这里必须用循环由于要让每一个生产者都知道本身要不要生产。若是不加就可能出现生产者唤醒生产者,而后连续两次生产。 try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name=name+"--"+count++; System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name); flag=true; this.notifyAll();//这里也要用notifyAll()不然,可能形成全部的线程都在等待。 } public synchronized void out(){ while(!flag){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } 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(); } } } public class ProducterConsumerDemo { 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(); } }
这是运行的结果。能够看出都是一个生产一个消费。ide
然而当jdk1.5以后,咱们能够用一些新特性去解决这个问题。java.util.concurrent.locks包下面的Lock接口,Condition接口。
this
这里其实Lock能够代替synchronized,而Condition所替代监视器。线程
这里并且Lock能够产生多个Condition监视器。对象
class Resource{ private int count=0; private String name; private boolean flag=false; Lock lock=new ReentrantLock(); Condition pro_condition=lock.newCondition();//这里是生产者的监视器 Condition con_condition=lock.newCondition();//这里是消费者的监视器 public void set(String name){ while(true) { try { lock.lock();//这里至关于代替了synchroinzed 代码块。这不过更人性,直接说上锁。 while (flag) { try { pro_condition.await();//可是应为await 会抛出异常但不是放锁,因此就必须在finally里释放锁 } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name + "--" + count++; System.out.println(Thread.currentThread().getName() + "-生产者.." + this.name); flag = true;//生产完成后将标志置为真! con_condition.signal(); //等价于this.notifyAll这里锁匙this对象 } catch (Exception e) { } finally { lock.unlock();//可是应为await 会抛出异常但不是放锁,因此就必须在finally里释放锁 } } } public void out(){ while(true) { lock.lock(); try { while (flag == false) { con_condition.wait();//即便有消费者线程被唤醒也必须判断flag的标志 } System.out.println(Thread.currentThread().getName() + "-消费者.." + name); flag = false; pro_condition.signal(); } catch (Exception e) { } finally { lock.unlock(); } } } } class Producer implements Runnable{ private Resource resource; Producer(Resource resource){ this.resource=resource; } public void run() { resource.set("商品"); } } class Consumer implements Runnable{ private Resource resource; Consumer(Resource resource){ this.resource=resource; } public void run() { resource.out(); } } public class ProducterConsumerDemo{ public static void main(String[] args) { Resource resource=new Resource(); Producer producer=new Producer(resource); Consumer consumer=new Consumer(resource); Thread p1=new Thread(producer); Thread p2=new Thread(producer); Thread c1=new Thread(consumer); Thread c2=new Thread(consumer); p1.start(); p2.start(); c1.start(); c2.start(); } }
这里能够看出,两个Condition对象,能够不用notifyAll()。生产者的锁须要消费者去打开,消费者能够用生产者打开。
blog