wait(); Make the thread wait and release the locks held by all objects;

Sleep (): puts a running thread into a sleeping state, which is a static method that catches InterruptException;

Notify (): Wakes up a thread in the wait state. Note that when this method is called, it is not possible to wake up a particular thread in the wait state. Instead, the JVM determines which thread to wake up, and not by priority.

NotifyAll () : Wakes up all wait threads, not giving all wakelines a lock on an object. Let them compete.

 

 

 

Threads in Java have four states:

Run, ready, suspend, end.

 

 

Synchronized and Java. Util. Concurrent. The locks. The similarities and differences between the Lock?

 

 

Main similarities: Lock can achieve all the functions achieved by synchronized Main differences: Lock has more precise thread semantics and better performance than synchronized.

Synchronized automatically releases locks, whereas locks must be released manually by the programmer and must be released ina finally clause.

 

 

 

 

 

How many ways can you implement a thread in Java? What keywords are used to modify synchronization methods? Why are the stop() and suspend() methods not recommended?

 

Stop () and suspend() are opposed and flags are recommended. There are two ways to implement it: inherit the Thread class and implement the Runnable interface with the synchronized keyword. Stop () is deprecated because it is unsafe. It releases any locks acquired by the thread, and if objects are in an incoherent state, other threads can examine and modify them in that state. As a result, it is difficult to detect the real problem. The suspend() method is prone to deadlocks. When suspend() is called, the target thread stops, but still holds the lock it acquired before. At this point, no other thread can access the locked resource unless the suspended thread resumes running. For any thread, trying to restore the target thread while trying to use any of the locked resources can cause a deadlock. Instead of using suspend(), you should put a flag in your Thread class indicating whether a Thread should be active or suspended. If the flag indicates that the thread should be suspended, wait() is used to tell it to wait. If the flag indicates that the thread should resume, the thread is restarted with a notify(). \

 

 

Thread status: Initial — ready — Run — terminated

               

The blocking state