Realized wait, notify, sleep, yield, join methods

Foreword

Before reading this article recommend that you look at, [Java memory model and volatile, the synchronized] ( www.jianshu.com/p/ad43ac258... Monitor lock) introduced in the Java virtual machine for each object implementation.

wait

  1. Methods can call wait out the current thread to block until the call notify or notifyAll method to make a thread to try to re-acquire the lock (ie wake up the thread).
  2. wait method is a local method, which is achieved by a monitor object lock, only those with the object monitor lock wait before calling the method, then how to call the wait method do?
  3. It is achieved by adding synchronized keyword, which is the reason why the wait must be run in synchronized modified code. But as long as the wait method calls, monitor lock will be immediately released.

notify

And wait Likewise must also be able to call in the code have synchronized keyword modifications, the same is the current object call.

sleep

  1. The method makes use of sleep thread is suspended, but also need the synchronized keyword, but just let out a slice of CPU time, and did not release the lock off the monitor.
  2. The thread is calling sleep in order to enter, when the current thread of execution is not yet complete, it is impossible to perform behind a thread synchronization method.
  3. When different wait method, because it calls immediately after the release of the lock off the monitor, so other threads can acquire the lock, calling the current object. When you call the wait method before the thread wakes up, it is to continue to participate in the competition lock (Note: This is not to say that can immediately acquire the lock is to be executed after obtaining a lock).

yield

  1. When a thread is executed, it has the highest priority.
  2. When calling the yield method, JVM will likely chance to run other threads have the same priority, it may not be made because the yield is only a suggestion and not forced to switch threads.
  3. When switching threads, the current yield is calling thread to be run state, rather than waiting or blocking state.

join

Blocking process is currently executing, the calling thread join method, after finished, go wake up the current thread.

Added: Why wait method in the Object class, Sleep in the Thread class?

Both acts are perpetrated is essentially different.

  1. sleep () is to allow a thread suspended for some time, its control is determined by the current thread, that is to say, the decision in the thread inside.
  2. wait () is defined by an object to call the
  • Both allow the thread to pause for some time, but essentially different is a thread running state control, is a problem of communication between threads

The difference between sleep and wait are:

  1. These two methods are from different classes Object and Thread
  2. The method is not the most important sleep release the lock, the lock release wait method, so that other threads may be used or a method of a synchronization control block.
  3. wait, notify, and notifyAll only synchronous or a synchronous control method for use inside the control block, and can be used anywhere sleep
   synchronized(x){
      x.notify()
     //或者wait()
   }
复制代码
  1. sleep must catch the exception, and wait, notify and notifyAll do not need to catch the exception

Guess you like

Origin juejin.im/post/5d6a0675f265da03bf0f5999