package zhangli1;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockDemo {
/**
* 生产者消费者实例的1.5以后版本中用Lock来实现。
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
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 count=1;
private static boolean flag=false;
Lock lock=new ReentrantLock();//创建一个锁对象
private Condition producer_con=lock.newCondition();//创建锁上的监视器对象(生产者)
private Condition consumer_con=lock.newCondition();//创建锁上的监视器对象(消费者)
public void set(String name){
lock.lock();
try{
while(flag)
try {
producer_con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name=name+count;
System.out.println(Thread.currentThread().getName()+".生产者.."+this.name);
count++;
consumer_con.signal();
}finally{
lock.unlock();
}
}
public void out(){
lock.lock();
try{
while(!flag)
try {
consumer_con.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"....消费者....."+name);
producer_con.signal();
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
Resource r;
Producer(Resource r){
this.r=r;
}
public void run(){
while(true){
r.set("生日蛋糕");
}
}
}
class Consumer implements Runnable{
Resource r;
Consumer(Resource r){
this.r=r;
}
public void run(){
while(true){
r.out();
}
}
}
运行结果如下,问下为什么只出现了两个线程,而没有Thread-2和Thread-3,而且为什么不是生产一个,消费一个的?
Thread-0.生产者..生日蛋糕79377
Thread-0.生产者..生日蛋糕79378
Thread-0.生产者..生日蛋糕79379
Thread-0.生产者..生日蛋糕79380
Thread-0.生产者..生日蛋糕79381
Thread-0.生产者..生日蛋糕79382
Thread-0.生产者..生日蛋糕79383
Thread-0.生产者..生日蛋糕79384
Thread-0.生产者..生日蛋糕79385
Thread-0.生产者..生日蛋糕79386
Thread-0.生产者..生日蛋糕79387
Thread-0.生产者..生日蛋糕79388
Thread-0.生产者..生日蛋糕79389
Thread-0.生产者..生日蛋糕79390
Thread-0.生产者..生日蛋糕79391
Thread-0.生产者..生日蛋糕79392
Thread-1.生产者..生日蛋糕79393
Thread-1.生产者..生日蛋糕79394
Thread-1.生产者..生日蛋糕79395
Thread-1.生产者..生日蛋糕79396
Thread-1.生产者..生日蛋糕79397
Thread-1.生产者..生日蛋糕79398
Thread-1.生产者..生日蛋糕79399
Thread-1.生产者..生日蛋糕79400
Thread-1.生产者..生日蛋糕79401
Thread-1.生产者..生日蛋糕79402
Thread-1.生产者..生日蛋糕79403
Thread-1.生产者..生日蛋糕79404
Thread-1.生产者..生日蛋糕79405
Thread-1.生产者..生日蛋糕79406
Thread-1.生产者..生日蛋糕79407
Thread-1.生产者..生日蛋糕79408
Thread-1.生产者..生日蛋糕79409
Thread-1.生产者..生日蛋糕79410
Thread-1.生产者..生日蛋糕79411
Thread-1.生产者..生日蛋糕79412
Thread-1.生产者..生日蛋糕79413
Thread-1.生产者..生日蛋糕79414
Thread-1.生产者..生日蛋糕79415
Thread-1.生产者..生日蛋糕79416
Thread-1.生产者..生日蛋糕79417
Thread-1.生产者..生日蛋糕79418
Thread-1.生产者..生日蛋糕79419
Thread-1.生产者..生日蛋糕79420
Thread-1.生产者..生日蛋糕79421
Thread-1.生产者..生日蛋糕79422
Thread-1.生产者..生日蛋糕79423
Thread-1.生产者..生日蛋糕79424
Thread-1.生产者..生日蛋糕79425
Thread-1.生产者..生日蛋糕79426
Thread-1.生产者..生日蛋糕79427
Thread-1.生产者..生日蛋糕79428
Thread-1.生产者..生日蛋糕79429
Thread-1.生产者..生日蛋糕79430
Thread-1.生产者..生日蛋糕79431
|