Java multithreading – control thread

Join thread

When the join () method of another thread is invoked in the execution stream of a thread, the calling thread will be blocked until the thread added by the join () method completes.

The join () method has three overloaded forms:

Background thread

The daemon thread is also called "daemon thread" or "daemon thread". The garbage collection thread of the JVM is a typical background thread.

About background threads:

Thread sleep

The sleep () method of thread class can make the executing thread pause for a period of time and enter the blocking state.

During the thread sleep period, even if there are no other runnable threads in the system, the threads in the sleep will not run.

Thread concession

Yield () is also a method of the thread class. Like sleep (), it can also pause a thread. However, it will not block the thread, but directly let the thread enter the ready state and wait for the scheduling of the system scheduler again.

In fact, when a thread calls the yield () method, only other threads in the ready state with the same priority as or higher than the current thread can get the opportunity to execute.

The difference between sleep() and yield():

thread priority

Each thread has a certain priority when executing. Threads with high priority get more execution opportunities, and threads with low priority get less execution opportunities.

Thread provides setpriority and getpriority methods to set and obtain thread priority.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>