Summarize the common methods of threading

table of Contents

One, join method

Two, interrupt method

Three, currentThread method

Four, isAlive method

Five, setDaemon method

Six, other methods

Seven, terminate the thread-without a disease

8. Terminating the thread-violently killed


One, join method

Join method: the thread executing this method enters the blocking state , and then turns from blocking to the ready state after the thread calling the method ends .

Note: The
thread object must call the start method before calling the join method , otherwise the thread will never enter the execution state .

Two, interrupt method

Interrupt method: End the blocking state of the thread in the process of calling the wait method of the Object class or the join method and sleep method of the class , and generate InterruptedException when the wait, join and sleep methods are called .

Theoretically, the difference between beforeTime and afterTime should be 30 seconds, but because the thread object executes the interrupt method after 1 second, the thread object ends the blocking state early, resulting in a difference of less than 30 seconds between beforeTime and afterTime

When the thread executes the line of code, the thread enters the blocking state; but the main thread executes the "counterThread.interrupt();" code after 10 seconds, which makes the thread blocking state end.

After the counter thread executes timeThread.join, it enters the blocking state. The time thread takes at least 30 seconds to end. After 15 seconds, the counter thread calls the interrupt method to cause the counter thread to end the blocking state early.

Three, currentThread method

currentThread method: returns the thread object currently being executed.

 

Four, isAlive method

isAlive method: Determine whether the thread is in a ready, running or blocked state, if it is, it returns true, otherwise it returns false.

Thread A executes the jion method called by "dead" thread B, then thread A will not block.

Five, setDaemon method

setDaemon method: used to set a thread that has not yet called the thread start method as a daemon thread. The daemon thread is mainly used to provide services for the running of other threads (the garbage collection mechanism in Java is the daemon thread), and this thread belongs to the thread that created it.
Note:
1. The daemon thread terminates with the termination of the last non-daemon thread, as shown in the following code:

The main thread ends, but because the timeThread non-daemon thread does not end immediately, the countThread daemon thread runs for a period of time; after the timeThread non-daemon thread ends, because there are no other non-daemon threads running in the process, the countThread daemon thread also stops running. Although the countThread guardian thread run method is an endless loop.

2. Other non-daemon threads started in the process will not end with the end of a certain non-daemon thread, as shown in the following code:

After a period of time, there is one less thread (time thread) in the javaw.exe process in the "Task Manager", but the counter thread is still working

3. The process ends with the end of the last non-daemon thread, the following code:

Description: Run the above program, the process ends with the end of the time thread (non-daemon thread).


Six, other methods

void start(): Start the thread , and the Java virtual machine is responsible for calling the thread's run() method. It is illegal to start a thread multiple times.
void sleep(long millis): Thread class static method , the thread enters the blocking state , and enters the ready state (Runnable) after the specified time (in milliseconds) is reached , instead of entering the execution state immediately. void yield(): A static method. The current thread gives up taking up CPU resources and returns to the ready state , so that other threads with a priority not lower than this thread have a chance to be executed. void setPriority(int newPriority): Set the priority of the current thread. The higher the thread priority, the more times the thread gets executed. The priority of the Java thread is represented by an integer. The value range is 1~10. The Thread class has the following three static constant: 1, static int MAX_PRIORITY    highest priority is 10; 2, static int NORM_PRIORITY    default priority value of 5; 3, static int MIN_PRIORITY    lowest priority value of 1; Note: multiple class created by the same thread Thread, the thread with the higher thread priority is most likely to get more execution times





; But for threads created by different thread classes, the higher the thread priority, the more execution times may not necessarily be, and the code complexity of this run method is related.
int getPriority(): Get the priority of the current thread.

Seven, terminate the thread-without a disease

8. Terminating the thread-violently killed

 

 

Guess you like

Origin blog.csdn.net/m0_46383618/article/details/113705106