在多生产者多消费者问题中,我们通过while判断和notifyAll()全唤醒方法解决了问题,但是notifyAll()同时也带来了弊端,它要唤醒所有的被等待的线程,意味着既唤醒了对方,也唤醒了本方,在唤醒本方线程后还要不断判断标记,就降低了程序的效率。我们希望只唤醒对方的线程。
在jdk进行升级后,就解决了类似的问题。
Lock(接口)实现提供了比使用synchronized方法和语句可获得的更广泛的锁定操作。
synchronized同步代码块格式:
Object obj=new Object();
void show()
{
synchronized(obj)
{
//需要被同步的代码
}
}
注:同步代码块,对于锁的操作是隐式的。
jdk1.5以后将同步和锁封装成了对象,并将操作锁的隐式方式定义到了该对象中,将隐式动作变成了显示动作。
下面采用jdk新特性实现Lock接口自定义锁实现同步:
Lock lock=new ReentrantLock(); //ReentrantLock类是Lock类的子类,实现Lock接口
void show()
{
try
{
lock.lock(); //获取锁
//代码
}
finally
{
lock.unlock(); //释放锁
}
}
import java.util.concurrent.locks.*; //Lock是其他包中的类,使用时需要导入
//资源
class Resource
{
private String name;
private int count=1;
private boolean flag=false; //设置标志位值为false,表示没有被生产
Lock lock=new ReentrantLock(); //实现一个接口
public void set(String name)
{
lock.lock();
try
{
while(flag) //while保证在此处被唤醒的线程重新判断标志位
try{this.wait();} catch(InterruptedException e){}
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag=true;
notifyAll(); //保证能够唤醒消费者线程
}
finally
{
lock.unlock();
}
}
public synchronized void out()
{
lock.lock();
try
{
while(!flag)
try{this.wait();} catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
flag=false;
notifyAll(); //保证能够唤醒生产者线程
}
finally
{
lock.unlock();
}
}
}
//生产者
class Producer implements Runnable
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
}
//消费者
class Consumer implements Runnable
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r=new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
//2个生产者,2个消费者
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
运行结果:
但是在上例中,我们用到的锁是lock,不再是this,而代码中调用wait()、notifyAll()方法的锁仍是this,就会出现错误,在jdk新特性中,用Lock来代替synchronized,用condition来代替监视器方法(wait()、notify()、notifyAll())。
condition接口:
interface Condition //Condition接口,替代Object类里的wait()、notify()、notifyAll()方法
{
await();
signal();
signalAll();
}
Lock lock=new ReectrantLock(); //一个锁上创建两个监视器,一个锁上就有多个监视器方法
Condition c1=lock.newCondition(); //每个监视器里都有await()、signal()、signalAll()方法
Condition c2=lock.newCondition();
import java.util.concurrent.locks.*; //Lock是其他包中的类,使用时需要导入
//资源
class Resource
{
private String name;
private int count=1;
private boolean flag=false; //设置标志位值为false,表示没有被生产
//创建一个锁对象
Lock lock=new ReentrantLock(); //实现一个接口
//通过已有的锁获取该锁上的监视器对象
Condition con=lock.newCondition(); //con是lock锁上的一个监视器
public void set(String name)
{
lock.lock();
try
{
while(flag) //while保证在此处被唤醒的线程重新判断标志位
//try{lock.wait();} catch(InterruptedException e){}
try{con.await();} catch(InterruptedException e){}
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag=true;
//notifyAll(); //保证能够唤醒消费者线程
con.signalAll();
}
finally
{
lock.unlock();
}
}
public synchronized void out()
{
lock.lock();
try
{
while(!flag)
//try{this.wait();} catch(InterruptedException e){}
try{con.await();} catch(InterruptedException e){}
System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
flag=false;
//notifyAll(); //保证能够唤醒生产者线程
con.signalAll();
}
finally
{
lock.unlock();
}
}
}
//生产者
class Producer implements Runnable
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
}
//消费者
class Consumer implements Runnable
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r=new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
//2个生产者,2个消费者
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
运行结果:
在前面我们提到希望只唤醒对方的线程,来提升程序运行的效率。如果程序中只有一个监视器,那个它能将生产者消费者全部wait,和生产者消费者全部唤醒。实际我们希望生产者唤醒消费者,消费者唤醒生产者,这样我们创建两组监视器,一组监视生产者,一组监视消费者。
import java.util.concurrent.locks.*; //Lock是其他包中的类,使用时需要导入
//资源
class Resource
{
private String name;
private int count=1;
private boolean flag=false; //设置标志位值为false,表示没有被生产
//创建一个锁对象
Lock lock=new ReentrantLock(); //实现一个接口
//通过已有的锁获取该锁上的监视器对象
//Condition con=lock.newCondition(); //con是lock锁上的一个监视器
//通过已有的锁获取两组监视器,一组监视生产者,一组监视消费者
Condition producer_con=lock.newCondition(); //生产者的监视器
Condition consumer_con=lock.newCondition(); //消费者的监视器
public void set(String name)
{
lock.lock();
try
{
while(flag) //while保证在此处被唤醒的线程重新判断标志位
//try{lock.wait();} catch(InterruptedException e){}
try{producer_con.await();} catch(InterruptedException e){} //生产者线程等待
this.name=name+count;
count++;
System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
flag=true;
//notifyAll(); //保证能够唤醒消费者线程
//con.signalAll();
consumer_con.signal(); //唤醒消费者的一个线程
}
finally
{
lock.unlock();
}
}
public synchronized void out()
{
lock.lock();
try
{
while(!flag)
//try{this.wait();} catch(InterruptedException e){}
try{consumer_con.await();} catch(InterruptedException e){} //消费者线程等待
System.out.println(Thread.currentThread().getName()+"...消费者......"+this.name);
flag=false;
//notifyAll(); //保证能够唤醒生产者线程
//con.signalAll();
producer_con.signal(); //唤醒生产者的一个线程
}
finally
{
lock.unlock();
}
}
}
//生产者
class Producer implements Runnable
{
private Resource r;
Producer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
}
//消费者
class Consumer implements Runnable
{
private Resource r;
Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r=new Resource();
Producer pro=new Producer(r);
Consumer con=new Consumer(r);
//2个生产者,2个消费者
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}
运行结果:
总结:
一:Lock接口:出现替代了同步代码块或者同步函数,将同步的隐式锁操作变成现实锁操作,同时更为灵活,可以一个锁上加上多组监视器。
方法:lock():获取锁
unlock():释放锁,通常需要定义在finally代码块中
二:Condition接口:出现替代了Object中 的wait notify notifyAll方法,将这些监视器方法单独进行了封装,变成Condition监视器对象,可以任意锁进行组合。
方法:await()
signal()
signalAll()