Java同步机制之notify vs notifyAll
2009-01-20 15:16
429 查看
use the wait-and-notify mechanism to deal with synchronized accessing a resource
wait-and-notify mechanism:
1 wait & notify can never be out of synchronized block of the releated-object (wait和notify方法必须在与之对应的对象的同步块里调用)
2 wait can release & get the lock automatically
the difference between notify and notifyAll
1 notify is wake the thread which invoke the wait-method
2 notifyAll is wake all the thread waiting for the lock released
3 why can form the thread invoking the wait-method and threads waiting for the lock released?
we know, if a synchronized method is invoked, all the other synchronized methods belong to the object can not be invoked, so the invoking threads must be blocked, and waiting the method can be invoke, so they all waite for the lock released in the list of waiting queue.
a synchronized class is the same;
when the notify method occurs, the waiting one who invoked wait() be wake;
when the notifyAll method occurs, the waiting queue choose the most prior one to wake, can be the one invoking wait(), also can be the ones waiting the lock released;
what we should be care?
if one waiting thread is waked, its waiting condition may not be satisfied. and in the right way, it should wait continuely.
how we assure this route?
--just as follows came from <Effective Java> and <Practical Java>
the former: Never invoke wait outside a loop
eg:
1
synchronized(obj) {
2
while(<condition does not hold>) {
3
wait();
4
// Perform action appropriate to condition
5
}
6
}
the later: Use spin locks to deal with wait and notifyAll
wait-and-notify mechanism:
1 wait & notify can never be out of synchronized block of the releated-object (wait和notify方法必须在与之对应的对象的同步块里调用)
2 wait can release & get the lock automatically
the difference between notify and notifyAll
1 notify is wake the thread which invoke the wait-method
2 notifyAll is wake all the thread waiting for the lock released
3 why can form the thread invoking the wait-method and threads waiting for the lock released?
we know, if a synchronized method is invoked, all the other synchronized methods belong to the object can not be invoked, so the invoking threads must be blocked, and waiting the method can be invoke, so they all waite for the lock released in the list of waiting queue.
a synchronized class is the same;
when the notify method occurs, the waiting one who invoked wait() be wake;
when the notifyAll method occurs, the waiting queue choose the most prior one to wake, can be the one invoking wait(), also can be the ones waiting the lock released;
what we should be care?
if one waiting thread is waked, its waiting condition may not be satisfied. and in the right way, it should wait continuely.
how we assure this route?
--just as follows came from <Effective Java> and <Practical Java>
the former: Never invoke wait outside a loop
eg:
1
synchronized(obj) {
2
while(<condition does not hold>) {
3
wait();
4
// Perform action appropriate to condition
5
}
6
}
the later: Use spin locks to deal with wait and notifyAll
相关文章推荐
- Java 同步方式 (2) —— wait和notify/notifyall
- Java 同步方式 (2) —— wait和notify/notifyall
- Java 同步方式 (2) —— wait和notify/notifyall
- Java 同步方式 (2) —— wait和notify/notifyall
- Java同步机制:synchronized,wait,notify
- Java多线程--同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期
- java 中的 wait/notify 同步锁机制
- Java 同步方式 (2) —— wait和notify/notifyall
- JAVA 的wait(), notify()与synchronized同步机制
- java并发包中的Condition和Lock 取代Synchronized、wait、notify/notifyAll实现线程的同步与互斥
- Java 同步方式 (1) —— synchronized 和 Java 同步方式 (2) —— wait和notify/notifyall (很全面)
- JAVA 的wait(), notify()与synchronized同步机制
- Java多线程---------同步与死锁:synchronized;等待与唤醒:wait、notify、notifyAll;生命周期
- Java同步机制:synchronized,wait,notify
- 【技术】JAVA 的wait(), notify()与synchronized同步机制
- Java同步机制:synchronized,wait,notify
- Java多线程中wait, notify and notifyAll的使用
- Java同步机制总结--synchronized
- java 三种调用机制(同步、回调、异步)
- 若还 不懂 java synchronized 同步,锁,对象wait,notify问题,看完这两段代码,解决问题。