In Java, there are three ways to terminate a running thread: Use the exit flag to cause the thread to exit gracefully, which means that when the Run method completes, the thread terminates using the Stop method to force the thread to terminate, but this method is not recommended because the stop is the same as suspend and resume, Are obsolete methods that can produce unpredictable results using the interrupt method, but the interrupt method does not terminate a normally running thread, it will simply set an interrupt tag that needs to be judged to stop the thread from running
There are two ways to determine if a thread is stopped: this.interrupted (): Test if the line has been interrupted, is a static method, the interrupt flag will be false this.isinterrupted (): Test whether the thread has been interrupted, non-static method, Break Flag not cleared
The Stop () method has been invalidated because it is possible to make some cleanup work impossible if the thread is forced to stop. Another situation is the locking of the object to "unlock", resulting in the data can not be synchronized processing, there is inconsistent data problems.
The suspend and Resume methods create the monopoly of a common synchronization object, such as the exclusive Printlnstream println method, which makes it impossible for other threads to access the public synchronization object. The use of these two methods also makes it easy to cause data to be unsynchronized because of a thread pause.
The lock on the keyword synchronized is an object lock, not a piece of code or function as a lock. So, if a thread first holds the lock lock of an object, a B-thread can call a method of a synchronized type in an object in an asynchronous way, but if a B thread wants to call a method of any synchronized type in the object, You need to wait, which is sync.
Synchronized has the function of locking a lock, that is, when using synchronized, when a thread gets an object lock, it can be locked again when the object lock is requested again. That is, you can get a lock when you call the other synchronized methods of this class inside a synchronized method. When an inheritance relationship exists between two classes, subclasses can invoke the synchronization method of the parent class through a "reentrant lock." When a thread executes a code that has an exception, its hold locks are automatically freed.
The synchronized (x) format is written with the X object itself as the object monitor, which results in the following three conclusions: synchronization effects when multiple threads execute synchronized (x) {} Synchronized code blocks simultaneously when other threads perform synchronization methods in the X object Synchronized when other threads execute the synchronized (this) code block inside the X object method
Synchronized is used on the static method to lock the entire class, which is applied to the objects of all classes, and synchronized (X.class). If a subclass overrides the Synchronized method of the parent class, the method of the subclass does not inherit its synchronized attribute, and still needs to explicitly add the synchronized flag.
Keyword volatile the main place to use is to perceive that the instance variable has been changed in multiple threads and to get the latest value usage, that is, when you read the instance variable with multithreading, you get the latest value citation. The volatile itself does not handle the atomicity of the data, but it forces the reading and writing of the data to affect the main memory. Keyword synchronized also has the ability to synchronize private variables in the work with variables in public memory.
The thread calls the Wait ()/notify () method before it must obtain an object-level lock on the object, or it throws a illegalmonitorstateexception run-time exception. After the Notify () method is executed, the current thread does not release the object lock immediately, and the current thread will not release the lock until the thread that calls the Notify () method finishes executing the program, that is, after exiting the synchronized code block.
Join () Waits internally using the Wait () method, while the Synchronized keyword uses the object monitor principle as synchronization. When a thread joins another thread (that is, waiting for another thread), the current thread throws an exception if the current thread (the thread waiting for another thread) is interrupted.
The join (long) is the time to set the wait, and the function of join (long) is implemented by a (long), so the join (long) method has the characteristic of releasing the lock. Sleep (long) does not release the lock.