线程同步会造成其他线程资源无法访问共享资源,者降低了对共享资源的访问效率,对于“”生成、消费“”模型,来说加强线程的通信可以是“生成和消费达到一个合理的状态”,即在没有产品时,及时同时生产,在生产过剩的时候通知及时消费。在java中可以通过调用Object类定义的wait(),notify()和notifyAll()方法。使线程之间相互通知事件的发生。要执行这些方法必须拥有相关对象的锁、
调用Wait()方法可以让线程等待,并释放对象锁,直到interupt()方法中断他或者另一个线程调用notify()或notifyall()通知他,wait()方法可以带一个参数表示等待的时间。使用此方式就不需要notify和notifyall的唤醒。此方法只能在一个同步方法中调用。
调用notify()方法时可以随机选择一个在该对象调用wait()方法的线程。解除他的阻塞。
notify和notifyall方法只能在同步方法或者同步快内部使用。
下面通过生成和消费模型演示线程通信机制的应用。
class Product{
int n;
//为true时 表示所有的值可取,为false时表示需要放进新值
boolean valueSet=false;
synchronized void get(){
//如果没有值,则等待新值放入
if(!valueSet){
try{
wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"-get"+n);
valueSet=false;
//通知等待线程,放入新值
notify();
}
synchronized void put(int n){
//如果有值,则等待线程取值
if(valueSet){
try{
wait();
}catch(Exception e){
}
}
this.n=n;
//将valueset的值设置成true,表示值已经放入
valueSet=true;
System.out.println(Thread.currentThread().getName()+"put:"+n);
notify();
}
}
class Producer implements Runnable{
Product product;
Producer(Product product){
this.product=product;
new Thread(this,"producer").start();
}
@Override
public void run() {
// TODO Auto-generated method stub
int k=0;
//生产5次
for(int i=0;i<5;i++){
product.put(k++);
}
}
}
class Consumer implements Runnable{
Product product;
Consumer(Product product){
this.product=product;
new Thread(this,"Consumer").start();
}
@Override
public void run() {
// TODO Auto-generated method stub
//消费5次
for(int i=0;i<5;i++){
product.get();
}
}
}
public class WaitDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Product product=new Product();
Producer producer=new Producer(product);
Consumer consumer=new Consumer(product);
}
}