sleep () and wait () of the difference? notify () and notifyAll () of the difference? start () and run () difference?

sleep () and wait () of the difference?

  • These two methods are from different classes Object and Thread
  • The method of sleep does not release the lock, the lock release wait method, so that other threads may be used or a method of a synchronization control block. wait, notify, and notifyAll only synchronous or a synchronous control method for use inside the control block, and can be used anywhere sleep
  • sleep must catch the exception, and wait, notify and notifyAll do not need to catch the exception
  • sleep (milliseconds) can be specified in time to make him wake up automatically, you can only call if less than interreput () to forcibly interrupt; wait () can notify () directly evoke.

notify () and notifyAll () of the difference?

Lock pool: suppose A thread already has an object (note: not the class) lock, while the other thread wants to call a synchronized method of this object (or synchronized block), since these threads in synchronized way into objects before you must first obtain ownership of the lock of the object, but the object's lock is currently owned by the thread a, so the thread enters the lock pool the object.

Wait pool: After a hypothesis A thread calls wait an object () method, thread A releases the lock of the object, into the pool to wait for the object

nitify () and notifyAll () the difference:

  • If a thread calls the object's wait () method, the thread will be in the object's waiting pool , the pool of waiting threads will not go to compete lock of the object .
  • When there is a thread calls the object  notifyAll () method (wake up all wait thread) or  the Notify () method (random only wake a wait thread), wakes up the thread goes to lock the pool of the object, lock the pool thread lock to compete for the object. In other words, as long as the call to notify a thread waiting to enter the pool by the lock pool, and notifyAll will move all the objects within the pool threads waiting to lock the pool, waiting for the lock to compete.
  • High priority threads competing to lock the probability of large objects, if a thread is no competition to the object lock, it will stay in the lock pool , the only thread calls again wait () method, it will return to the waiting pool in. The competition subject to lock down the thread execution continues until the execution is over synchronized block of code, it will free up the object lock, then lock the thread pool will continue to compete the object lock.

start () and run () difference?

run () method entry corresponding to the task thread processing logic, it is called directly by the Java virtual machine running in the respective thread, not called by the application code.

The role of start () is to start the appropriate thread . Start a thread is actually request the Java virtual machine running the corresponding thread, when the thread can run is determined by the thread scheduler. start () call does not mean the end of the corresponding thread has been running, the thread may run later, it may never run.

Guess you like

Origin www.cnblogs.com/jxxblogs/p/11653038.html