Communication and collaboration thread: sleep, wait, notify, yield, join relationship and differences

一、sleep、join、yield、wait、notify、notifyAll

1, sleep ()

 makes the current thread (ie the thread of the method call) suspended for some time, so other threads have the opportunity to continue to implement, but it does not release the object lock. This means that if there are synchronized synchronous fast, other threads still can not access the shared data. Note that this method to catch the exception.

 For example two threads execute simultaneously (without the synchronized) MAX_PRIORITY a thread priority, the other is MIN_PRIORITY, if not Sleep () method, only after the high priority thread is finished, the low priority thread can execute; however after high priority thread sleep (500), a low priority have the opportunity to be executed.

 In short, sleep () may be a low priority threads get a chance to execute, of course, can make the same priority, high-priority thread has a chance to execute.

2, the Join ()

 the Join () method to make a thread calls this method before this is finished, the thread is waiting for the implementation of the method is completed and then continue down. Note that this method also needs to catch exceptions.

3, yield ()

 This method sleep () is similar, but can not be specified by the user, how long to pause, and yield () method can only give the same priority thread has a chance to execute.

4, wait () and notify (), notifyAll ()

 These three methods for coordinating multiple threads to access shared data, it must be used in the synchronized block. let other threads when synchronized keyword is used to protect the shared data, prevent other threads access to shared data, but processes such program on the very flexible, how can not exit the synchronized block in the current thread also have the opportunity to access shared data it? At this time, three methods to use flexibly control.

 wait () method to suspend the current thread releases the object lock and marked, so that other threads can enter the synchronized block, the current thread is waiting for the pool into the object. When the call to notify () method, the object is removed from the pool waiting for an arbitrary thread pool and into the lock flag wait, just wait for the lock symbol in the pool thread can acquire the lock flag; if the lock flag wait no threads in the pool , then notify () does not work.

 notifyAll () from the object to wait for the pool to remove all threads and wait for that object into the lock flag wait pool.

 Note These three methods are methods of java.lang.Object.

Two, run and start ()

 the code to be processed into the run () method, start () method starts the thread will automatically call the run () method, which is defined by java memory mechanisms. And the run () method must be public access, the return type of void.

Third, the synchronized keyword

 This keyword is used to protect shared data, of course, a prerequisite is to distinguish between what data is shared data. Each object has a lock flag, when one thread to access the object, the Synchronized modified data will be "locked" to prevent other threads access. The current thread after the completion of this part of the data access releases the lock symbol, other threads can be visited.

Four, wait () and notify (), notifyAll () method of class Object, sleep () and yield () method of the Thread class.

(1), there is a method commonly used wait wait () and wait (long timeout);

 former void wait () call this object in the other thread notify () method or the notifyAll () method causes the current thread to wait.

 void wait (long timeout) calls this object before in other threads notify () method or the notifyAll () method, or a specified amount of time, leading to the current thread to wait.

 After the wait (), the thread will be freed in its possession "lock flag" so that other shnchronized where the data objects in the thread can be used by other threads.

 wait () h and notify () because the "lock flag" will be the object of the operation, so they must be called in Synchronized function or synchronized block in. If you call a function in non-synchronized or non-synchronized block, although able to compile, but at runtime IllegalMonitorStateException the exception occurs. .

(2), Thread.sleep (long millis ) must be provided with a time parameter.

 sleep (long) the current thread to enter a standstill, so execution sleep () thread certainly will not be executed within a specified time;

 sleep (long) allows low-priority thread gets the opportunity to perform, of course, you can make the same priority thread has a chance to execute;

 SLEEP (Long) is not going to release the lock flag.

(3), yield () no arguments

 sleep method the thread to sleep for a while in the current run, can not run into the state, the length of this period is set by the program, yield method makes the current thread yields the CPU possession, but let the time is not set.

 yield () will not release the lock symbol.

 Indeed, the yield () method corresponding to the following operations; first detect whether the current thread is the same with priority runnable state, if any, to put the CPU possession times thread, or continue to run the original thread, yield () method is called "concession", which give the opportunity to run the same level of the other threads.

 sleep method allows lower priority thread gets the opportunity to run, but when the yield () method executes, the current thread is still in an operable state, it is impossible to make a lower priority thread CPU at this time to obtain possession. In a running system, if a higher priority thread does not call sleep method, nor by the I / O blocking, then the lower priority thread can only wait for all higher-priority thread to finish, before a chance to run .

 yield () Causes the current thread just returned to an executable state, all threads executing yield () is likely to be executed immediately after entering into an executable state, so the yield () method can only have the same priority thread execution chance.

Guess you like

Origin www.cnblogs.com/windpoplar/p/11827833.html