The synchronized method controls access to class member variables: each class instance corresponds to a lock, and each synchronized method must obtain the lock of the class instance that calls the method before execution, otherwise the thread is blocked, once the method is executed, the lock is exclusive until the lock is released when the method is returned. Then, the blocked thread can obtain the lock and re-enter the executable status.
Wait ()/notify (): calls the wait () method of any object to cause thread blocking and the lock on this object is released. However, calling the notify () method of any object causes the wait () method of the object to be called () method ).
Relationship between synchronized and wait () and notify ()
1. Where synchronized exists, wait and notify may not be available.
2. with wait, synchronized must be used in zooy. this is because wait and notify are not thread classes, but methods that every object has. Moreover, these two methods are related to the object lock. Where there is a lock, there must be synchronized.
In addition, note that if you want to use the notify and wait methods together, you must first call notify and then call wait, this thread is no longer the current thread.
Note: It is best to use the while clause before calling the wait () method, instead of the IF clause. The while clause can be used to determine conditions again after being wakeup, while the if clause can only be judged once;
Four States of a thread
1. New status: the thread has been created but not executed (START () has not been called ).
2. executable status: the thread can be executed, although not necessarily in progress. The CPU time may be allocated to this thread at any time for execution.
3. Death state: Normally, running () returns to cause the thread to die. Calling stop () or destroy () has the same effect, but is not recommended. The former produces exceptions, and the latter forces termination without releasing the lock.
4. Blocking status: the thread will not be allocated CPU time and cannot be executed.
First of all, all the methods described above belong to the Thread class, but this pair (wait ()/notify () is directly affiliated to the object class, that is, all objects have this pair of methods. This seems incredible at the beginning, but it is actually quite natural, because the lock occupied by this pair of methods is released when blocking, and the lock is owned by any object and calls any object
The wait () method causes thread blocking and the lock on the object is released. However, calling the notify () method of any object causes the wait () method of the object to be called () method ).
Secondly, all the methods described above can be called anywhere, but this pair of methods must be called in the synchronized method or block for a very simple reason, the lock can be released only when the current thread occupies the lock in the synchronized method or block.
In the same way, the lock on the object that calls this method must be owned by the current thread so that the lock can be released. Therefore, this pair of method calls must be placed in such a synchronized method or block. The lock object of this method or block is the object that calls this pair of methods. If this condition is not met, the program can still be compiled, but the illegalmonitorstateexception will occur at runtime.
Exception.
The preceding features of the wait () and notify () methods determine that they are often used together with the synchronized method or block, by comparing them with the inter-process communication machine of the operating system, we will find their similarity: the synchronized method or block provides functions similar to the operating system primitive, their execution will not be affected by the multi-thread mechanism, and this method is equivalent
Block and wakeup primitives (both methods are declared as synchronized ). The combination of these algorithms allows us to implement a series of exquisite inter-process communication algorithms (such as semaphore algorithms) on the operating system and solve various complicated inter-thread communication problems. About
The wait () and notify () methods are further described as follows:
First, the thread that calls the notify () method to remove blocking is randomly selected from the thread that is blocked by calling the wait () method of the object, we cannot predict which thread will be selected, so we should be very careful when programming to avoid problems caused by such uncertainty.
Second: In addition to notify (), there is also a method notifyall () that can also play a similar role. The only difference is that calling the notifyall () method will call the wait () and all the blocked threads are all blocked at one time. Of course, only the thread that gets the lock can enter the executable state.
When talking about blocking, we can't help but talk about deadlocks. A brief analysis shows that calls of the suspend () method and the wait () method without specifying the timeout period may lead to deadlocks. Unfortunately, Java does not support avoiding deadlocks at the language level. We must be careful in programming to avoid deadlocks.
We have analyzed various methods for implementing thread blocking in Java. We have focused on the wait () and notify () methods, because they have the most powerful functions, they are also the most flexible to use, but this also leads to low efficiency and error-prone. In actual use, we should use various methods flexibly to better achieve our goal.
Daemon thread
A daemon is a special type of thread. It differs from a common thread because it is not the core part of an application. When all the non-daemon threads of an application terminate the operation, even if a daemon thread is still running, the application will be terminated. Otherwise, the application will not be terminated as long as a non-daemon thread is running. Daemon threads are generally used to provide services for other threads in the background.
You can call the isdaemon () method to determine whether a thread is a daemon thread, or call the setdaemon () method to set a thread as a daemon thread.
Remarks: This article Reprinted from: http://hi.baidu.com/%B0% AE %CE%D2%B5%C4%D6%B4%D7%C5/blog/item/7b2869fd6a185d1e08244d1e.html