java中wait和notify

    在JAVA中,是没有类似于PV操作、进程互斥等相关的方法的。JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的互斥内存块,在JAVA中的Object类型中,都是带有一个内存锁的,在有线程获取该内存锁后,其它线程无法访问该内存,从而实现JAVA中简单的同步、互斥操作。明白这个原理,就能理解为什么synchronized(this)与synchronized(static XXX)的区别了,synchronized就是针对内存区块申请内存锁,this关键字代表类的一个对象,所以其内存锁是针对相同对象的互斥操作,而static成员属于类专有,其内存空间为该类所有成员共有,这就导致synchronized()对static成员加锁,相当于对类加锁,也就是在该类的所有成员间实现互斥,在同一时间只有一个线程可访问该类的实例。如果只是简单的想要实现在JAVA中的线程互斥,明白这些基本就已经够了。但如果需要在线程间相互唤醒的话就需要借助Object.wait(), Object.nofity()了。

    Obj.wait(),与Obj.notify()必须要与synchronized(Obj)一起使用,也就是wait,与notify是针对已经获取了Obj锁进行操作,从语法角度来说就是Obj.wait(),Obj.notify必须在synchronized(Obj){...}语句块内。从功能上来说wait就是说线程在获取对象锁后,主动释放对象锁,同时本线程休眠。直到有其它线程调用对象的notify()唤醒该线程,才能继续获取对象锁,并继续执行。相应的notify()就是对对象锁的唤醒操作。但有一点需要注意的是notify()调用后,并不是马上就释放对象锁的,而是在相应的synchronized(){}语句块执行结束,自动释放锁后,JVM会在wait()对象锁的线程中随机选取一线程,赋予其对象锁,唤醒线程,继续执行。这样就提供了在线程间同步、唤醒的操作。Thread.sleep()与Object.wait()二者都可以暂停当前线程,释放CPU控制权,主要的区别在于Object.wait()在释放CPU同时,释放了对象锁的控制。

 

    单单在概念上理解清楚了还不够,需要在实际的例子中进行测试才能更好的理解。对Object.wait(),Object.notify()的应用最经典的例子就是生产者-消费者模式,实现一个如下:

public class Test {
	static Apple apple = new Apple();
	public static void main(String[] args) {
//		for (int i=0; i<10; i++) {
			new ProducerThread(apple).start();
			new CustomerThread(apple).start();
//		}
	}
}

class ProducerThread extends Thread {
	Apple apple;
	public ProducerThread(Apple apple) {
		this.apple = apple;
	}
	
	@Override
	public void run() {
		try {
			for (int i=0;i<10;i++) {
				apple.addApple();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class CustomerThread extends Thread {
	Apple apple;
	public CustomerThread(Apple apple) {
		this.apple = apple;
	}
	
	@Override
	public void run() {
		try {
			for (int i=0;i<10;i++) {
				apple.removeApple();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class Apple {
	int count = 0;
	synchronized void addApple() throws InterruptedException {
		if (count > 0) {
			System.out.println("producer begin wait");
			wait();
			System.out.println("producer after wait");
		} else {
			System.out.println("producer add apple");
			count ++;
			notify();
			System.out.println("producer notify after add apple");
		}
	}
	
	synchronized void removeApple() throws InterruptedException {
		if (count <= 0) {
			System.out.println("customer begin wait");
			wait();
			System.out.println("customer after wait");
		} else {
			System.out.println("customer remove apple");
			count --;
			notify();
			System.out.println("customer notify after remove apple");
		}
	}
}

输出结果:

producer add apple
producer notify after add apple
producer begin wait
customer remove apple
customer notify after remove apple
customer begin wait
producer after wait
producer add apple
producer notify after add apple
producer begin wait
customer after wait
customer remove apple
customer notify after remove apple
customer begin wait
producer after wait
producer add apple
producer notify after add apple
producer begin wait
customer after wait
customer remove apple
customer notify after remove apple
customer begin wait
producer after wait
producer add apple
producer notify after add apple
producer begin wait
customer after wait
customer remove apple
customer notify after remove apple
customer begin wait
producer after wait
producer add apple
producer notify after add apple
producer begin wait
customer after wait
customer remove apple
customer notify after remove apple
customer begin wait
producer after wait

posted @ 2014-07-23 18:19  JerryShao  阅读(1316)  评论(0编辑  收藏  举报