Wait lock example

public class WaitDemo {
    private static Object locker = new Object();

    public static void main(String[] args) throws InterruptedException {
        WaitDemo waitDemo = new WaitDemo();
  // Start a new thread to prevent the main thread from sleeping  new Thread(() -> {  try {  waitDemo.doWait();  } catch (InterruptedException e) {  e.printStackTrace();  }  }).start();  Thread.sleep(200); // Wait () is executed before notify()  waitDemo.doNotify();  }   / * ** implement wait ()* /  private void doWait(a) throws InterruptedException {  synchronized (locker) {  System.out.println("wait start.");  locker.wait();  System.out.println("wait end.");  }  }   / * ** perform notify ()* /  private void doNotify(a) {  synchronized (locker) {  System.out.println("notify start.");  locker.notify();  System.out.println("notify end.");  }  } } Copy the code

The execution results of the above programs are as follows:

wait start.

notify start.

notify end.

wait end.

Code parsing

The wait() lock is set to the same locker as the notify() lock. The notify() lock is released after the wait() lock is called. If the lock is not released, notify() code is not executed. This is confirmed by the printed results, so wait() releases the lock.

Example of sleep locking

public class WaitDemo {
    private static Object locker = new Object();

    public static void main(String[] args) throws InterruptedException {
        WaitDemo waitDemo = new WaitDemo();
 // Start a new thread to prevent the main thread from sleeping  new Thread(() -> {  synchronized (locker) {  try {  System.out.println("sleep start.");  Thread.sleep(1000);  System.out.println("sleep end.");  } catch (InterruptedException e) {  e.printStackTrace();  }  }  }).start();   Thread.sleep(200);  waitDemo.doNotify();  }   / * ** perform notify ()* /  private void doNotify(a) {  synchronized (locker) {  System.out.println("notify start.");  locker.notify();  System.out.println("notify end.");  }  } } Copy the code

The execution results of the above programs are as follows:

sleep start.

sleep end.

notify start.

notify end.

Code parsing

You can see from the above code that the sleep(1000) method (line number: Sleep (1000) is executed after sleep(1000) is executed. Sleep (1000) is executed after sleep(1000) is executed, so sleep(1000) is not released.

Knowledge extension

1. What’s the difference between sleep and wait?

Sleep and wait are common interview questions, but it’s not always easy to get them right.

Common answers to questions about the difference between sleep and wait are as follows:

  • Wait must be used with synchronize, but sleep does not.
  • Threads in wait state can be woken up by notify and notifyAll threads, while threads in sleep state cannot be woken up by notify.
  • A wait is usually executed conditionally, and a thread stays in wait until some condition is true, but sleep simply puts your thread to sleep;
  • The wait method releases object locks, but the sleep method does not.

However, the above answer obviously leaves out an important difference. After the wait method is called, the thread will become WATING, while after the sleep method is called, the thread will become TIMED_WAITING.

2. Can wait be used in static methods? Why is that?

Wait methods cannot be used in static because they are instance methods.

public final void wait(a) throws InterruptedException {
    wait(0);
}
Copy the code

3. Can you use “wait/notify” without synchronized? Why is that?

No, because synchronized is not used with the program will report an error, as shown below:

The deeper reason is that without synchronized, Lost Wake Up Problem can be caused. Details can be seen: juejin.cn/post/684490…

conclusion

In this paper, we test the wait and sleep methods by synchronized locking the same object, and prove that the wait method releases the lock, while the sleep method does not. We’ve also covered several common interview questions to ask in the hope that this article will help you.

Follow the public account “Java Chinese Community” for more exciting content.