First, the thread state transition
②t.join(): Refers to waiting for t thread to complete execution, join is Thread A method of the class, which is called directly after starting the thread, that is, the function of join() is: "waiting for the thread to terminate", what needs to be understood here is that the thread refers to the main thread waiting for the termination of the child thread. That is, the code after the child thread calls the join() method can only be executed after the child thread ends.
sleep() and yield() The difference between sleep() and yield()): sleep() causes the current thread to enter a stagnant state, so the thread executing sleep() will definitely not be executed within the specified time; yield() ) just makes the current thread return to the executable state, so the thread executing yield() may be executed again immediately after entering the executable state.
The sleep method makes the currently running thread sleepy for a period of time and enters an inoperable state. The length of this period of time is set by the program. The yield method makes the current thread give up the CPU ownership, but the given time cannot be set. . In fact, the yield() method corresponds to the following operations: first check whether there are currently threads of the same priority in the same runnable state, if so, give the CPU ownership to this thread, otherwise, continue to run the original thread. So the yield() method is called "backing off", it gives the running opportunity to other threads of the same priority.
In addition, the sleep method allows lower priority threads to get the running opportunity, but when the yield() method is executed, the current thread is still It is in a runnable state, so it is impossible to give a lower priority thread some time to take the CPU. In a running system, if the higher-priority thread does not call the sleep method and is not blocked by I\O, then the lower-priority thread can only wait for all higher-priority threads to finish before they have a chance to run .
MIN_PRIORITY = 1
NORM_PRIORITY = 5
MAX_PRIORITY = 10
⑥wait()
Obj.wait(), and Obj.notify() must be used together with synchronized(Obj), that is, wait, and notify is to operate on the Obj lock that has been acquired, from a grammatical point of view, it is Obj.wait(), Obj.notify must be inside a synchronized(Obj){...} block . Functionally, wait means that the thread actively releases the object lock after acquiring the object lock, and the thread sleeps at the same time. Until another thread calls the object's notify() to wake up the thread, it can continue to acquire the object lock and continue to execute. The corresponding notify() is the wake-up operation on the object lock. But one thing to note is that after the notify() call, the object lock is not released immediately, but after the execution of the corresponding synchronized(){} statement block ends, after the lock is automatically released, the JVM will wait() the object lock Randomly select a thread among the threads, give it an object lock, wake up the thread, and continue to execute. This provides synchronization and wake-up operations between threads. Both Thread.sleep() and Object.wait() can suspend the current thread and release the CPU control. The main difference is that Object.wait() releases the control of the object lock while releasing the CPU.
common:
1. They are both in a multi-threaded environment, and they can block the specified number of milliseconds at the call of the program and return.
2. Both wait() and sleep() can interrupt the suspended state of the thread through the interrupt() method, so that the thread immediately throws InterruptedException.
If thread A wants to end thread B immediately, it can call the interrupt method on the Thread instance corresponding to thread B. If thread B is waiting/sleep/join at the moment, thread B will throw InterruptedException immediately, and the thread can be safely terminated by returning directly in catch() {}.
It should be noted that InterruptedException is thrown internally by the thread itself, not by the interrupt() method. When interrupt() is called on a thread that is executing normal code, the thread never throws an InterruptedException. However, as soon as the thread enters wait()/sleep()/join(), an InterruptedException is thrown immediately.
Differences:
1. Thread class methods: sleep(), yield(), etc.
Object methods: wait() and notify(), etc.
2. Each object has a lock to control synchronous access. The Synchronized keyword can interact with the object's lock to achieve thread synchronization.
The sleep method does not release the lock, while the wait method releases the lock so that other threads can use the synchronized control block or method.
3. wait, notify and notifyAll can only be used in synchronous control methods or synchronous control blocks, while sleep can be used anywhere
4. sleep must catch exceptions, while wait, notify and notifyAll do not need to catch exceptions
so sleep() and wait The biggest difference between () method is:
when sleep() sleeps, it keeps the object lock and still holds the lock;
while wait() sleeps, it releases the object lock.
However, both wait() and sleep() can interrupt the suspended state of the thread through the interrupt() method, so that the thread immediately throws InterruptedException (but this method is not recommended).
The sleep() method
sleep() makes the current thread enter a stagnant state (blocks the current thread), and gives up the use of the CPU. The purpose is to prevent the current thread from occupying the CPU resources obtained by the process alone, so as to leave a certain time for other threads to execute. Opportunity;
sleep() is the Static method of the Thread class; therefore, it cannot change the machine lock of the object, so when the Sleep() method is called in a Synchronized block, although the thread sleeps, the machine lock of the object does not change. The wood has not been released, and other threads cannot access the object (the object lock is held even if it is asleep).
After the sleep() sleep time expires, the thread may not execute immediately because other threads may be running and not scheduled to give up unless the thread has a higher priority.
wait() method
The wait() method is a method in the Object class; when a thread executes the wait() method, it enters a waiting pool related to the object, and at the same time loses (releases) the object's machine lock (temporarily loses the machine lock). lock, the object lock needs to be returned after the wait (long timeout) timeout period); other threads can access;
wait() uses notify or notifyAll or the specified sleep time to wake up the threads in the current waiting pool.
wiat() must be placed in a synchronized block, otherwise a "java.lang.IllegalMonitorStateException" will be thrown at program runtime.
sleep(): Force a thread to sleep for N milliseconds.
isAlive(): Determines whether a thread is alive.
join(): Wait for the thread to terminate.
activeCount(): The number of active threads in the program.
enumerate(): Enumerates threads in a program.
currentThread(): Get the current thread.
isDaemon(): Whether a thread is a daemon thread.
setDaemon(): Sets a thread as a daemon thread. (The difference between user threads and daemon threads is whether to wait for the main thread to end depending on the end of the main thread)
setName(): Set a name for the thread.
wait(): Forces a thread to wait.
notify(): Notifies a thread to continue running.
setPriority(): Sets the priority of a thread.
1) Within an object instance, synchronized aMethod(){} can prevent multiple threads from accessing the synchronized method of this object at the same time (if an object has multiple synchronized methods, as long as A thread accesses one of the synchronized methods, other threads cannot access any of the synchronized methods in this object at the same time). At this time, the synchronized methods of different object instances do not interfere with each other. That is to say, other threads can still access synchronized methods in another object instance of the same class at the same time;
2) is the scope of a class, synchronized static aStaticMethod{} prevents multiple threads from accessing synchronized static methods in this class at the same time. It works on all object instances of the class.
2. In addition to using the synchronized keyword before the method, the synchronized keyword can also be used in a block in the method, indicating that only the resources of this block are mutually exclusive access. The usage is: synchronized(this){/*block*/}, its scope is the current object;
3. The synchronized keyword cannot be inherited, that is, the method synchronized f(){} of the base class is inherited The class is not automatically synchronized f(){}, but becomes f(){}. Inheriting a class requires you to explicitly specify one of its methods as a synchronized method;