How to correctly use object methods in Java: wait and notify
In Java, wait and notify are important methods used to achieve collaboration between multiple threads. Correct use of these two methods can avoid multi-threading problems such as race conditions and deadlocks, and ensure the safety and efficiency of the program. This article will introduce how to correctly use the object methods wait and notify in Java and provide specific code examples.
1. The basic principles and functions of wait and notify
Before understanding how to use wait and notify correctly, you need to understand their basic principles and functions.
2. Steps to use wait and notify correctly
To use wait and notify correctly, you need to follow the following steps:
3. Specific code examples
The following code examples show how to correctly use wait and notify to achieve collaboration between two threads. One thread is responsible for waiting to be awakened, and the other thread is responsible for waking up.
public class WaitNotifyExample { private static final Object lock = new Object(); private static boolean isReady = false; public static void main(String[] args) { Thread waitingThread = new Thread(() -> { synchronized (lock) { while (!isReady) { try { System.out.println("等待中..."); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("等待结束!"); } }); Thread notifyingThread = new Thread(() -> { synchronized (lock) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("唤醒等待的线程..."); isReady = true; lock.notify(); } }); waitingThread.start(); notifyingThread.start(); } }
Run the above code, you can see that the waiting thread will enter the waiting state and continue execution after being awakened.
In this example, we use a shared boolean variable isReady to represent the waiting condition. The waiting thread will check isReady before entering the critical section. If the condition is not met, the wait method will be called to enter the waiting state. After the wake-up thread meets the condition, it will change the value of isReady and call the notify method to wake up the waiting thread.
It should be noted that when using wait and notify, you must ensure that the thread acquires the lock of the same object. Otherwise, the thread cannot properly enter the wait state or be awakened.
Summary
Using the wait and notify methods can achieve collaboration between multiple threads and ensure the safety and efficiency of the program. When using these two methods, you need to follow the correct steps, including locking, calling the wait method, checking conditions, releasing the lock, calling the notify method, etc. By rationally using the wait and notify methods, we can avoid multi-threading problems and improve the reliability and maintainability of the program.
The above is the detailed content of How to correctly use object methods wait and notify in Java. For more information, please follow other related articles on the PHP Chinese website!