Java multithreading learning road (two) --- detailed explanation of related classes and methods

Java multithreading learning road (two)-detailed explanation of related classes and methods

2.1 The creation of thread classes in Java

To generate a thread class, there are two methods in java:

  • Implement the Runnable interface and override the run() method.The code in run() is the task of the current thread.
  • Inherit the Thread class

Under normal circumstances, we create thread classes by implementing the Runnable interface, because Java can only implement single inheritance, but multiple interfaces can be implemented.

  • Starting a thread through Thread.start() results in the execution of the run() method, so can the run() method be called directly?

    You can directly call the run() method of the Thread object, but this will not start another thread, and it will still be in the Main thread.

2.2 Get the return value through the Callable interface

2.3 State transition of thread class

Thread state transition diagram

2.4 Detailed explanation of common methods:

1. Thread.start() : Start the thread .

After creating a Thread class, calling the start() function will start the thread and make it enter the Runnable state. Essentially, the start() method will call the run() method. It is
more recommended to use **executor (executor)** to start one Thread. This method uses the thread pool technology to simplify our operations, and it provides an intermediate layer between the client and task execution.

2.Thread.join():

Add another thread to the current thread, the current thread will be suspended and enter the Waiting state until the execution of the added thread is completed . If Thread1 calls Thread2.join() on Thread2, then Thread1 will be suspended until Thread2 The execution is complete. The join() method can be interrupted by the interrupt() method .

3.Thread.join(long n)

The basic effect is the same as the join() method without parameters. The current thread enters Timed_Waiting, but when the set time is reached, if the added thread has not ended, it will be ignored. Continue to run.

static Thread.sleep(long miliseconds):

Put the current thread to sleep for a period of time .

  • Calling the sleep() method in the current thread will make the thread enter the TIMED_WAITING (timeout waiting) state. The parameter is the waiting time, the unit is miliseconds. After JDK1.5, a more explicit version TimeUnit.MILLISECONDS.sleep(long n) was introduced . More readable. More recommended
  • Entering the sleep() state can be interrupted. You only need to call the interrupt() method of the sleeping thread object to interrupt it, but an InterruptedException will be thrown.

Thread.yield():

Give up the occupation of resources, resources will take the initiative to get it out .yield have a yield in English, give up, give way mean, here also happens to be mean. Calls yield () method is to thread scheduler one suggestion , like It is saying: "I have completed the important part, you can go to execute other threads, don't worry about me", but I emphasized that this is just a suggestion, it is not certain whether the thread scheduler will listen to it.

Thread.interrupt():

Set the interrupt flag of the called Thread object to True. For example, when the thread1.interrupt() method is called in the main thread, the interrupt flag in thread1 is set to true at this time. But the called thread will not stop . It already knows that there are other threads that want to interrupt it, and it will take certain measures. When calling this method, the called thread may have two situations

  • It is in Runnable state
  • It is in Waiting, Timed_Waiting, Blocked state. At this time, it will throw InterruptedException and clear the interrupted flag , that is, the interrupted flag is false at this time;

Thread.isInterrupted():

Get the interrupt flag of the current thread

static Thread.interrupted()

Thread.setDaemon(Boolean)/Thread.isDaemon():

Set the current thread to Daemon (when the parameter is true) and get the current thread status

Thread.getId():

Get the id of the current thread

Thread.getName()/Thread.setName():

Get and set thread name

Thread.getState():

Get the current thread status, that is, the 6 types

Thread.isAlive();

Get whether the current thread is alive

static Thread.currentThread():

Get a reference to the current thread

setPriority()/getPriority():

Set and get the priority of the current thread . The priority of the thread will pass the thread importance to the thread scheduler . The scheduler will tend to let threads with high priority execute. But threads with low priority will also have the opportunity to execute just The concept is small ( that is, priority does not cause deadlock ). In most cases, all threads should run at the default priority. Trying to manipulate the priority of threads is usually a wrong behavior.
Defined in the JDK 10 priority levels, but it does not map well with most OSs. So when adjusting the priority level, only use MAX_PRIORITY, NORM_PRIORITY, MIN_PRIORITY three levels

2.5 Executor

2.5.1 Executor concept diagram

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109549569