Original address: http://dylanxu.iteye.com/blog/1322066
1. Sleep ()
Causes the current thread (that is, the thread that called the method) to suspend execution for a period of time, allowing other threads to continue executing, but it does not release the object lock.
This means that if there is synchronized synchronization fast, other threads still cannot access the shared data. Note that the method is catching an exception.
For example, there are two threads executing at the same time (without synchronized) one thread has a priority of max_priority and the other is min_priority,
Without the Sleep () method, a low-priority line friend can execute after the high-priority thread has finished executing, but a high-priority thread, Sleep (500), has a chance to execute the low priority.
In summary, sleep () allows a low-priority thread to get the chance to execute and, of course, allow the same priority, high-priority threads to execute.
2. Join ()
The join () method causes the thread that invokes the method to complete before the thread that waits for the method finishes executing. Note that this method also requires catching exceptions.
3. Yield ()
This method is similar to sleep (), except that it is not possible for the user to specify how long to pause, and the yield () method only allows the same priority thread to have an opportunity to execute.
4. Wait () and notify (), Notifyall ()
These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within the synchronized statement block. The Synchronized keyword is used to protect shared data,
Prevents other threads from accessing the shared data, but the process of the program is not flexible enough to allow other lines to be made when the current thread has not exited the synchronized data block
Opportunities to access shared data? At this point, use these three methods to control flexibly.
The wait () method suspends the current thread and releases the object lock, allowing other threads to enter the synchronized data block, where the current thread is placed in the object waiting pool.
When the Notify () method is called, an arbitrary thread is removed from the object's waiting pool and placed in the lock flag waiting pool, and only the lock flag waits for the threads in the pool to acquire the lock flag;
If the lock flag waits for no threads in the pool, notify () does not work.
Notifyall () removes all the threads waiting for that object from the object waiting pool and puts it in the lock flag waiting pool.
Note that these three methods are java.lang.Object methods.
Second, run and start ()
Put the code that needs to be processed into the run () method, and the start () method will automatically call the run () method, which is defined by the Java memory mechanism.
and the run () method must be public access, and the return value type is void.
Third, the key word synchronized
This keyword is used to protect shared data, but the prerequisite is to distinguish which data is shared. Each object has a lock flag, and when a thread accesses the object,
Data modified by synchronized will be "locked" and blocked from being accessed by other threads. When the current thread finishes accessing this piece of data, the lock flag is released and other threads can access it.
Four, Wait () and notify (), Notifyall () are methods of the object class, and sleep () and yield () are methods of the thread class.
(1), the usual wait method has wait () and wait (long timeout);
void Wait () causes the current thread to wait before other threads call the Notify () method of this object or the Notifyall () method.
void wait (long timeout) causes the current thread to wait before another thread calls this object's notify () method or the Notifyall () method, or exceeds the specified amount of time.
After wait (), the thread releases the "lock flag" that it occupies, so that other shnchronized data in the object where the thread resides can be used by other threads.
Wait () H and notify () because they operate on the object's "lock flags," they must be called in the synchronized function or synchronized block.
If the call is made in the non-synchronized function or non-synchronized block, the illegalmonitorstateexception exception will occur at run time, although it can be compiled.
(2), Thread.Sleep (long Millis) must have a time parameter.
Sleep (long) puts the current thread into a stagnant state, so the thread executing sleep () will not be executed for a specified period of time;
Sleep (long) allows the thread with the lower priority to be executed and, of course, the opportunity to execute with the same priority thread;
Sleep (long) does not release the lock flag.
(3), yield () no parameters
The Sleep method causes the currently running thread to be asleep for a period of time, entering the non-operational state, the length of time is set by the program, and the yield method gives the current thread the CPU possession,
But the time to surrender is not set.
Yield () also does not release the lock flag.
In fact, the yield () method corresponds to the following operation, first detects whether the thread with the same priority is in the same operational state, and if so, gives the CPU possession to the secondary thread, or continues
Runs the original thread, so the yield () method is called "concession", which gives the run opportunity to other threads of the same rank.
The Sleep method allows a lower-priority thread to get a run opportunity, but when the yield () method executes, the current thread is still in a running state, so it is not possible to let the lower-priority thread receive
Take CPU possession. In a running system, if a higher-priority thread does not call the sleep method and is not blocked by I/O, the lower-priority thread can wait for all higher-priority
The thread runs to the end before it has a chance to run.
Yield () simply brings the current thread back to the executable state, and all threads that execute yield () are likely to be executed immediately after entering the executable state, so the yield () method can only make the same priority
Threads have an opportunity to execute
[Reproduced]java yield (), sleep (), wait () the difference between the detailed