object wait notify 实例一则
2016-07-05 11:18
387 查看
其实,在Java中任何一个对象均有一个锁,而syhchronized同步块在调用时会检测该对象的锁定情况,主过程和子过程两个线程竞争同一对象时,在主过程中调用wait()方法,子过程即可进入synchronized同步块。而当子过程调用notify方法时,主过程又重新获得了对象锁,继续执行下去。实现如下:
package com.unclepeng.test;
//主过程
public class MainProgross2 {
private static byte[] sign = new byte[0];
public static void main(String[] args) {
SubProgross subProgross = new SubProgross();
Thread subThread = new Thread(subProgross);
long currentTime = System.currentTimeMillis();
System.out.println("execute main progross!");
subThread.start();
synchronized(sign){
System.out.println("main progross get lock!");
try {
sign.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("leave main progross!");
long endTime = System.currentTimeMillis();
System.out.println("total time: " + (endTime - currentTime));
}
//子过程
static class SubProgross implements Runnable{
public void run() {
System.out.println("execute sub progross!");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(sign){
System.out.println("sub progross get lock!");
sign.notify();
}
System.out.println("leave sub progross!");
}
}
}
其实在Java的Object类中notifyAll、notify、wait方法均为native方法,在windows中是通过外部的dll来实现其内部,可以说Object的notify及wait等操作是JVM级的,而第一种方法是程序级的,理论上来说,后一种要更高效。
package com.unclepeng.test;
//主过程
public class MainProgross2 {
private static byte[] sign = new byte[0];
public static void main(String[] args) {
SubProgross subProgross = new SubProgross();
Thread subThread = new Thread(subProgross);
long currentTime = System.currentTimeMillis();
System.out.println("execute main progross!");
subThread.start();
synchronized(sign){
System.out.println("main progross get lock!");
try {
sign.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("leave main progross!");
long endTime = System.currentTimeMillis();
System.out.println("total time: " + (endTime - currentTime));
}
//子过程
static class SubProgross implements Runnable{
public void run() {
System.out.println("execute sub progross!");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized(sign){
System.out.println("sub progross get lock!");
sign.notify();
}
System.out.println("leave sub progross!");
}
}
}
其实在Java的Object类中notifyAll、notify、wait方法均为native方法,在windows中是通过外部的dll来实现其内部,可以说Object的notify及wait等操作是JVM级的,而第一种方法是程序级的,理论上来说,后一种要更高效。
相关文章推荐
- SQL Abstraction and Object Hydration
- The ServiceClass object does not implement the required method in the following form
- Swift学习之和Objective-C的联系与区别
- mysql到hbase数据迁移之getobject
- jni object的使用
- 利用ObjectAnimator实现ArcMenu
- Objective-C面向对象初步归纳
- objective-c 编程基础(三 面向对象编程)
- Swift Any & AnyObject
- objective-c 编程基础(二)
- html内容写入到文件中的时候出现‘TypeError: expected a character buffer object’错误
- Objective-C-UI控件学习之UITableView详解
- Javascript Object, Set, Map总结
- Objective-C学习之NSMutableArray中arraywithcapacity 和 initwithcapacity的区别?
- ios中数组的 firstObject 和 [0]的区别
- csharp: Procedure with DAO(Data Access Object) and DAL(Data Access Layer)
- ObjectARX安装小结
- 错误:a different object with the same identifier value was already associated with the session
- Part6 继承与OOD Inheritance and Object-Oriented Design(三)
- objective-c 与 swift 类型互转实例