在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

最新文章

  1. 敏捷开发与jira
  2. 我的CS考研路
  3. Android成长日记-ListView
  4. fedora23忘记root密码怎么办??
  5. SQL server 数据库连接方式分析
  6. emberjs重写补充类之reopen方法和reopenClass方法
  7. javaWeb 使用jsp标签进行防盗链
  8. Eclipse上安装springsource-tool-suite(zhuan)
  9. 在没有spineRunTime情况下手动使用spine数据
  10. 使用gulp、yeoman、bower建站
  11. 经典FormsAuthenticationTicket 分析
  12. Linux多线程编程(不限Linux)
  13. MySQL 元数据
  14. CoreData Multiple Context性能分析-读书笔记
  15. UCOSII时间任务块
  16. Webpack的加载器
  17. [poj2185]Milking Grid_KMP
  18. Struts 1 之&lt;logic&gt;标签库
  19. openstack——删除网络
  20. left join on and 与 left join on where的区别

热门文章

  1. jQuery on() 和 live
  2. 解决maven无法下载依赖的jar包的问题
  3. Windows下使用Nexus搭建Maven私服(安装)
  4. axis2调用WSDL接口
  5. TongWeb
  6. 使用母版页时内容页如何使用css和javascript
  7. 使用crontab定时执行脚本时别忘了输出重定向&gt;
  8. postgresql 导出建表语句的方法-类似describe table
  9. 设计模式C++实现——工厂方法模式
  10. ngnix