2021-02-18Java Learning Record

Java basics-multithreading (Thread.join throws InterruptedException)

Insert picture description here

When a method is declared after it may throw an InterruptedException, it means that the method may take a while, but it can be cancelled.

The representative methods of throwing InterruptedException are:

  • The wait method of the java.lang.Object class
  • The sleep method of java.lang.Thread class
  • join method of java.lang.Thread class

Methods that take some time

  • The thread executing the wait method will enter the waiting area and wait to be notified/notify All. During the waiting period, the thread will not be active.

  • The thread that executes the sleep method will suspend the execution of the time set in the parameter.

  • The thread executing the join method will wait until the specified thread ends.

Can be cancelled

Because the time-consuming operation will reduce the responsiveness of the program, it is possible to cancel/abandon the execution of this method in the middle. Here is mainly canceled by the interrupt method.

  • sleep method and interrupt method

The interrupt method is an instance method of the Thread class. It does not need to acquire the lock of the Thread instance during execution. Any thread can call the interrupt method of other threads through the thread instance at any time.
When the thread in sleep is called the interrupt method, it will give up the suspended state and throw an InterruptedException. In this way, the control of the thread is given to the catch block that catches this exception.

  • wait method and interrupt method

When the thread calls the wait method, the thread will touch the lock when it enters the waiting area. When the interrupt method is called on the thread in wait, the lock will be reacquired, and then the InterruptedException will be thrown. Before the lock is acquired, the InterruptedException cannot be thrown.

  • join method and interrupt method

When a thread waits for the end of other threads in the join method, the interrupt method can also be used to cancel it. Because the join method does not need to acquire a lock, it will immediately jump to the catch block like sleep

What does the interrupt method do?
The interrupt method actually only changes the interrupt state. The sleep, wait, and join methods will constantly check the value of the interrupt state, and throw InterruptEdException by themselves.

Therefore, if the interrupt method is called while the thread is performing other processing, the thread will not throw InterruptedException. Only when the thread goes to sleep, wait, join these methods, it will throw InterruptedException. If sleep, wait, join methods are not called, or the interrupt status is not checked in the thread, and InterruptedException is thrown by yourself, then InterruptedException will not be thrown.

The isInterrupted method can be used to check the interrupt status

The Thread.interrupted method can be used to check and clear the interrupted status.

Java basics-multithreading (similarities and differences of setting priority for main thread and sub-thread)

Main thread:

  • Get the main thread name: Thread.currentThread().getName()
  • Get the priority of the main thread: Thread.currentThread().getPriority()

Sub-threading:

  • Get the thread name: object name.getName()
  • Get thread priority: object name.getPriority()

Set the priority of the sub-thread and the main thread

  • Object name.setPriority(Thread.MAX_PRIORITY);//Sub-thread setting
  • Thread.currentThread().setPriority(Thread.MIN_PRIORITY);//Main thread setting

Insert picture description here

Java basics-multithreading (in inheriting the Thread mode, how to share the number of votes when buying tickets)

Solution: Set the private attribute of the ticket to static;

例如:private static int ticket = 100;

Note: Only needed for inheriting Thread mode. And realize the default sharing in Runnable! Do not set to static

Java basics-multithreading (implementation of the source code understanding of t1.start() in the thread creation method of the Runnable interface)

Source code understanding of t1.start():

  • Start thread
  • Call the run() method of the current thread

Insert picture description here
Question: It is obvious that the run() method in Thread is called, but how to call the run() method in the Runnable interface implementation class? ----Look at the source code

Source code 1: run() in
Insert picture description here
Thread Source code 2: target source code in Thread
Insert picture description here
As can be seen from the above figure: target is of type Runnable
Insert picture description here
. The Thread constructor when the object is created from the above figure

Guess you like

Origin blog.csdn.net/m0_51755061/article/details/113841620