Home >类库下载 >java类库 >What methods does thread class have?

What methods does thread class have?

清浅
清浅Original
2019-04-08 13:42:4031531browse

Methods in the Thread class can be divided into instance methods and static methods. Instance methods include start() method, run() method, etc. Static methods include currentThread() method, sleep(long millis) ) methods, etc.

What methods does thread class have?

[Recommended course: Java Tutorial]

in the Thread class Instance method

start() method

The function of this method is to notify the thread planner that the scene can be run. It should be noted that the order in which the start method is called does not represent the order in which threads are started, that is, the CPU executes the code of which thread is uncertain.

run() method

This method is executed after the thread class calls start. If you call run directly instead of the start method, it will be the same as the ordinary method. The difference

isAlive() method

is to determine whether the current thread is active. The active status means that it has been started but not terminated.

getPriority() and setPriority(int newPriority) methods

These two methods are used to get and set the priority of the current thread. Threads with higher priority get more CPU. In other words, among the two waiting threads, the thread with higher priority is easier to be executed by the CPU. By default, the thread's priority is 5. The priority of threads is divided into levels from 1 to 10.

isDaeMon, setDaemon(boolean on) method

There are two types of java threads, one is the user thread and the other is the daemon thread. The daemon thread is a special thread. Any daemon thread is the nanny of all non-daemon threads in the jvm. When there are no non-daemon threads in the process, the daemon thread will be automatically destroyed. A typical daemon thread is a garbage collection thread.

The first method is to determine whether the thread is a daemon thread. The second method is to set the thread as a daemon thread. It must be setDaemon(true) before the thread starts

interrupt() Method

Using this method will not interrupt the thread. In fact, the actual function of calling interrupt is to throw an interrupt signal when the thread is blocked, so that the thread can exit the blocked state.

join() method

The join method will cause the thread calling the join method to block indefinitely until the thread calling the join method is destroyed. The join method uses wait internally. (), so the lock will be released.

Static method of Thread class

currentThread() method

This method returns a reference to the currently executing thread object .

sleep(long millis) method

The function of the sleep method is to sleep the executing thread at the specified time. The lock is not released.

yield() method

Pauses the currently executing thread object and executes other threads. This pause will give up cpu resources, and the time of giving up is uncertain

Method calling method in Thread class

When calling the method in Thread, in the thread class, There are two methods:

this.xxx() method

The thread in this method is the thread instance itself

Thread.currentThread .xxx() or Thread.xxx() mode

This indicates the thread executing the code block where Thread.currenThread.xxx() is located.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone.

The above is the detailed content of What methods does thread class have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is Spring bootNext article:What is Spring boot