Common operations of Java concurrent threads

1. Terminate the thread

1. Thread.stop()
The stop() method of the Thread class is deprecated. Reason: When the thread is terminated, the thread will be terminated directly, and the lock held by this thread will be released immediately. If the writing thread writes half of the data and is forcibly terminated, it may cause the object to be written bad.
2. Using an exit marker
3. Thread.interrupt()

2. Thread interruption

Interrupting the thread does not cause the thread to exit immediately, it instructs the thread to send a notification that someone wants you to exit.

public void Thread.interript()                 // 通知目标线程中断,并设置中断标志位
public boolean Thread.isInterrupt()            // 判断是否被中断
public static boolean Thread.interrupted()     // 判断是否被中断,并清除当前中断状态标志
//由于中断会抛出异常,但是此时,它会清除中断标志位  
public static native sleep(long millis) throws InterruptedException  

3. Wait and notify

public final void wait() throws InterruptedException
public final native void notify() 

These two methods are in the Object class. They must be included in the corresponding synchronized statement, and both require first obtaining a monitor of the target object.

Difference between Object.wait() and Thread.sleep():

  1. You can make the thread wait for a certain amount of time.
  2. wait can be woken up, it will release the lock of the target object, enter the wait pool of the object, and call the notify() method to enter the wait pool. sleep will not release any resources, it will automatically resume after the sleep time expires, and the thread will return to the ready state.
  3. sleep must catch exceptions, while wait, notify and notifyAll do not need to catch exceptions. If the exception is not caught, the thread will terminate abnormally

write picture description here

4. Wait for the thread to end (join) and yield (yield)

1. join: waiting between threads

public final void join() throws InterruptedException //无线等待,直到目标线程执行完毕
public final synchrozied void join(long millis) throws InterruptedException  //最大等待

// join核心代码,本质上是调用wait()在当前线程对象实例上
while(isAlive) {
    wait(0);
}

For join, it makes the calling thread wait on the current thread object. When the thread finishes executing, the waiting thread will call notifyAll() to notify all waiting threads to continue executing before exiting. So you need to pay attention not to use methods like wait() or notify() on the Thread object instance, so as not to affect each other.

2. yield: The role of yield between threads
is to give up the resources of the current CPU and give it to other thread areas to occupy the execution time of the CPU.

// 使当前线程让出CPU,进入就绪状态
public static native void yield();

The difference between the sleep() method and the yield() method of a thread

  • The sleep() method does not consider the priority of the thread when it gives other threads a chance to run, so it will give lower priority threads a chance to run; the yield() method will only give threads of the same priority or higher priority to run opportunity;
  • The thread enters the blocked state after executing the sleep() method, and enters the ready state after executing the yield() method;
  • The sleep() method declares that InterruptedException is thrown, while the yield() method does not declare any exception;
  • The sleep() method is more portable than the yield() method (related to operating system CPU scheduling).

5. Suspend and resume

Both of these methods have been marked as deprecated and deprecated.
Reason : suspend() causes the thread to suspend execution without releasing any resources. When other threads want to acquire the lock occupied by it, they cannot acquire it, resulting in failure of normal operation. Until resume() is executed on the corresponding thread.

6, stationed in the background: daemon thread (Daemon)

public final void setDaemon(boolean on) {
        checkAccess();
        if (isAlive()) {
            throw new IllegalThreadStateException();
        }
        daemon = on;
}

A daemon thread is a special thread that is the guardian of the system, silently completing some system services in the background, such as garbage collection, JIT thread, etc.
Corresponding to it is the user thread, which is the system worker thread. If all user threads end, then the object to be guarded by the daemon thread no longer exists, and the entire application should end naturally. When there are only daemon threads in a Java application, the virtual machine will automatically exit.
Note : Judging from isAlive(), setDaemon() must be called before start().

7. Thread priority

   /**
     * The minimum priority that a thread can have.
     */
    public final static int MIN_PRIORITY = 1;

   /**
     * The default priority that is assigned to a thread.
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10;

Java uses 1 to 10 by default for thread priority.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325159708&siteId=291194637