2024. 10. 15. 13:29ㆍJAVA & SPRING/자바
Methods
wait()
- When wait() method is called, the calling thread stops its execution until notify(), notifyAll() method is invoked by some other Thread.
notify()
- It wakes only one thread.
notifyAll()
- It wakes up all the threads on the same object.
sleep()
- It causes a thread to sleep for a specified amount of time.
Why must the wait method be used in code protected by synchronized?
Before the thread calls the wait method, it is suspended by the scheduler, so the wait method has not yet been executed. So, the notify has no effect because the consumer thread's wait method has not been executed yet and there is no thread waiting to be awakened.
public class QueueDemo extends Thread {
Queue<String> buffer = new LinkedList<String>();
public void save(String data){
buffer.add(data);
notify();
}
public String take() throws InterruptedException {
while (buffer.isEmpty()) {
wait();
}
return buffer.remove();
}
}
That is why if you want to execute both method without synchronized, "Current thread is not owner", the error occurs since a program makes sure that this kind of problems are not appeared.
Why does the wait method need to be used in loop operations?
After a thread calls the wait method, there is a possibility of a spurious wakeup, i.e., the thread may be woken up without being called by notify/notifyAll, without being interrupted, or without a timeout, which is something we dont want to happen.
Although the probability of a false wakeup is very small in real-world environments, the program still needs to ensure correctness in the event of a false wakeup, so it needs to use the structure of a while loop.
It this way, even if it is falsely awakened, the coditions in 'while' will be checked again. If the conditions are not met, 'wait' will continue, thus eliminating the risk of false awakenings.
What are the similarities and differences between the wait/notify and sleep methods?
The similarities
1. They can both block threads.
2. They can respond to interrupt interrupts: If they receive an interrupt signal during the waiting process, they can respond and throw an InterruptedException exception.
The differences
1. The wait method must be used in synchronized protected code, while the sleep method does not have this requirement.
2. When the sleep method is executed in synchronized code, it does not release the monitor lock, but the wait method actively release the monitor lock.
3. The sleep method requires a time to be defined, and the thread will resume actively after the time expires. For the wait method without parameters, it means permanent waiting until it is interrupted or awakened. It does not resum actively.
4. wait/notify are methods of the Object class, while sleep is a method of the Thread class.
'JAVA & SPRING > 자바' 카테고리의 다른 글
| Java Concurrency - Thread Priority (1) | 2024.10.20 |
|---|---|
| Java Concurrency - Thread Group (1) | 2024.10.19 |
| AtomicInteger, Concurrent Collections 알아보기 (1) | 2024.08.27 |
| Java Concurrent 패키지 (0) | 2024.08.26 |
| Array (0) | 2024.06.16 |