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