php生成多个消费者,多个生产者多个消费者

package function.thread;

import java.util.Vector;

public class MultiProConsumTest {

public static void main(String args[]) {

Vector obj = new Vector();

Thread consumer1 = new Thread(new MultiConsumer(obj));

Thread consumer2 = new Thread(new MultiConsumer(obj));

Thread consumer3 = new Thread(new MultiConsumer(obj));

Thread producter1 = new Thread(new MultiProducter(obj));

Thread producter2 = new Thread(new MultiProducter(obj));

Thread producter3 = new Thread(new MultiProducter(obj));

consumer1.start();

consumer2.start();

consumer3.start();

producter1.start();

producter2.start();

producter3.start();

}

}

/* 消费者 extends不能写在implements后面 */

class MultiConsumer extends MultiShareInfo implements Runnable {

public MultiConsumer(Vector v) {

this.obj = v;

}

public void run() {

synchronized (obj) {

System.out.println("******Consumer:【"+Thread.currentThread().getName()+"】 get a lock *****");

String str = "";

try {

if (obj.size() == 0) {

System.out.println("******Consumer:【"+Thread.currentThread().getName()+"】 wait begin *****");

obj.wait();//醒来之后会执行接下去的语句,此时已经没有了锁

}

System.out.println("******Consumer:【"+Thread.currentThread().getName()+"】wake up and get obj size: " + obj.size());

//wait被唤醒之后,就独占锁继续执行

if(obj.size() == 0){

System.out.println("******Consumer:【"+Thread.currentThread().getName()+"】still hold the lock********");

for(int i=0;i<50;i++){

Thread.sleep(100);

System.out.print(".");

}

System.out.println();

}else{

str = (String)obj.get(obj.size()-1);

obj.remove(obj.size()-1);

System.out.println("******Consumer:【"+Thread.currentThread().getName()+"】 get 【"+str+"】");

obj.notifyAll();//唤醒了所有等待obj的线程,包括生成者和消费者线程

}

} catch (Exception e) {

e.printStackTrace();

}

}//synchronize block

}

}

/* 生产者 */

class MultiProducter extends MultiShareInfo implements Runnable {

public MultiProducter(Vector v) {

this.obj = v;

}

public void run() {

synchronized (obj) {

System.out.println("******Producter:【"+Thread.currentThread().getName()+"】 get a lock *****");

int size = obj.size();

try {

if (obj.size() >=10) {

obj.wait();

}

obj.add(new String("apples"+(size+1)));

System.out.println("******Producter:【"+Thread.currentThread().getName()+"】 put an apple,next notify&sleep 500 ms");

obj.notifyAll();

Thread.sleep(500);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

class MultiShareInfo{

public Vector obj;

}

运行结果:

******Consumer:【Thread-0】 get a lock *****

******Consumer:【Thread-0】 wait begin *****

******Consumer:【Thread-2】 get a lock *****

******Consumer:【Thread-2】 wait begin *****

******Consumer:【Thread-1】 get a lock *****

******Consumer:【Thread-1】 wait begin *****

******Producter:【Thread-4】 get a lock *****

******Producter:【Thread-4】 put an apple,next notify&sleep 500 ms

******Producter:【Thread-5】 get a lock *****

******Producter:【Thread-5】 put an apple,next notify&sleep 500 ms

******Consumer:【Thread-1】wake up and get obj size: 2

******Consumer:【Thread-1】 get 【apples2】

******Consumer:【Thread-2】wake up and get obj size: 1

******Consumer:【Thread-2】 get 【apples1】

******Consumer:【Thread-0】wake up and get obj size: 0

******Consumer:【Thread-0】still hold the lock********

..................................................

******Producter:【Thread-3】 get a lock *****

******Producter:【Thread-3】 put an apple,next notify&sleep 500 ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值