The thread that called the wait() method is in a WAITING state. The thread that called the wait() method will be WAITING for another thread to notify or interrupt. Therefore, wait methods are typically used in synchronized methods or synchronized blocks of code.

Sleep causes the current thread to sleep. Different from wait,sleep does not release the currently occupied lock, and sleep(long) causes the thread to enter the TIMED WATING state. The wait() method causes the current thread to enter the WATING state

3. Yield (yield) Yield causes the current thread to yield CPU slices, recompeting with other threads for CPU slices. In general, threads with higher priority have a higher chance of successfully competing for CPU time slices, but this is not absolute, and some operating systems are insensitive to thread priority.

例 句 : Interrupting a thread is intended to give the thread a notification signal that affects an interrupt flag bit inside the thread. The thread itself does not change state (blocking, terminating, etc.).

1. Calling interrupt() does not interrupt a running thread. In other words, threads in the Running state will not be terminated because they are interrupted, only the internal maintenance of the interrupt flag bit is changed. 2. If a thread is in the TIMED WATING state when sleep() is called, interrupt() throws InterruptedException and the TIMED WATING state is terminated prematurely.

3. Many methods that declare InterruptedException (such as Thread.sleep(long Mills method)) clear the interrupt identifier before throwing an exception, so calling isInterrupted() returns false after throwing an exception. 4. The interrupt state is an inherent identifier of a thread that can be safely terminated. For example, if you want to terminate a thread by calling thread.interrupt(), you can terminate the thread gracefully within the thread’s run method based on the value of thread.isinterrupted ().

Call the Join () method of one thread in the current thread, then the current thread becomes blocked and returns to the end of another thread. Then the current thread changes from the blocked state to the ready state, waiting for the FAVOR of the CPU.

6. Why use join()? In many cases, the main thread generates and starts the child thread and needs the result returned by the child thread. That is, the main thread needs to terminate after the child thread has terminated. In this case, join() is used.

7. The notify() method in the Object class wakes up a single thread that is waiting on the monitor of this Object. If all threads are waiting on the monitor of this Object, one of the threads is chosen to wake up. Wait on an object’s monitor until the current thread waives the lock on the object before continuing execution of the awakened thread, which competes in the usual manner with all other threads actively synchronizing on the object. A similar method is notifyAll(), which wakes up all threads waiting on the monitor again.

8. Other methods:

  1. Sleep () : forces a thread to sleep N milliseconds.
  2. IsAlive () : checks whether a thread isAlive.
  3. Join () : Waits for the thread to terminate.
  4. ActiveCount () : The number of active threads in the program.
  5. Enumerate () : Threads in an enumeration program.
  6. CurrentThread () : gets the currentThread.
  7. IsDaemon () : whether a thread is a daemon thread.
  8. SetDaemon () : Sets a thread as a daemon thread. (The difference between a user thread and a daemon thread is whether to wait for the main thread to terminate depending on the completion of the main thread.)
  9. SetName () : Sets a name for the thread.
  10. Wait () : Forces a thread to wait.
  11. Notify () : Notifies a thread to continue running.
  12. SetPriority () : Sets the priority of a thread.
  13. GetPriority (): : Gets the priority of a thread.