目录
线程生命周期
Thread类的常用方法
构造方法
静态方法
常用实例方法
线程有其创建、就绪、运行、阻塞、死亡的过程,将其称之为“线程的生命周期”。如下图所示,
对应以上5个状态,jdk-Thread类的源码中定义了枚举类State,用于描述这几个状态,
public enum State {/*** Thread state for a thread which has not yet started.新生状态:对应new Thread()的操作,但还并未调用start()方法来启动子线程。 */NEW,/*** Thread state for a runnable thread. A thread in the runnable* state is executing in the Java virtual machine but it may* be waiting for other resources from the operating system* such as processor.可运行状态:指的是线程创建好之后,在等待系统资源的状态。*/RUNNABLE,/*** Thread state for a thread blocked waiting for a monitor lock.* A thread in the blocked state is waiting for a monitor lock* to enter a synchronized block/method or* reenter a synchronized block/method after calling* {@link Object#wait() Object.wait}.阻塞状态:指的是线程等待重新获取同步锁(monitor lock),尝试进入/再次进入synchronized同步方法/同步代码块时的状态。*/BLOCKED,/*** Thread state for a waiting thread.* A thread is in the waiting state due to calling one of the* following methods:**
**- {@link Object#wait() Object.wait} with no timeout
*- {@link #join() Thread.join} with no timeout
*- {@link LockSupport#park() LockSupport.park}
*A thread in the waiting state is waiting for another thread to* perform a particular action.** For example, a thread that has called Object.wait()* on an object is waiting for another thread to call* Object.notify() or Object.notifyAll() on* that object. A thread that has called Thread.join()* is waiting for a specified thread to terminate.(无限)等待状态:进入等待状态。调用Object.wait、Thread.join()、LockSupport.park方法,会导致其进入等待状态。*/WAITING,/*** Thread state for a waiting thread with a specified waiting time.* A thread is in the timed waiting state due to calling one of* the following methods with a specified positive waiting time:*
*
(有限)等待状态:等待指定的时长。调用Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil会进入此状态。*/TIMED_WAITING,/*** Thread state for a terminated thread.* The thread has completed execution.死亡状态:线程体run()方法已经被执行完毕。*/TERMINATED; }- {@link #sleep Thread.sleep}
*- {@link Object#wait(long) Object.wait} with timeout
*- {@link #join(long) Thread.join} with timeout
*- {@link LockSupport#parkNanos LockSupport.parkNanos}
*- {@link LockSupport#parkUntil LockSupport.parkUntil}
*
Thread类的常用方法如下,
其中:name为线程名称;target为包含线程体的目标对象(Runnable接口对象)。
Thread()
Thread(String name)
Thread(Runnable target)
Thread(Runnable target, String name)
currentThread():返回当前正在执行的线程;
interrupted():返回当前执行的线程是否已经被中断;
sleep(long millis):使当前执行的线程睡眠多少毫秒数;
yield():调用此方法的线程T,将让出T所占用的同步锁资源(较少使用)。
/*** A hint to the scheduler that the current thread is willing to yield* its current use of a processor. The scheduler is free to ignore this* hint.向调度器暗示——调用yield()方法的线程T,自愿让出CPU处理器资源,回到等待队列中,与其它线程一起参与新一轮的同步锁竞争。但是,调度器也可以忽略这个暗示。** Yield is a heuristic attempt to improve relative progression* between threads that would otherwise over-utilise a CPU. Its use* should be combined with detailed profiling and benchmarking to* ensure that it actually has the desired effect.yield是一种试探性的尝试,可以避免过度使用CPU。因此,它的使用应该与详细的配置分析和基准测试相结合,以确保它实际上具有预期的效果(即:较少使用此方法)。**
It is rarely appropriate to use this method. It may be useful* for debugging or testing purposes, where it may help to reproduce* bugs due to race conditions. It may also be useful when designing* concurrency control constructs such as the ones in the* {@link java.util.concurrent.locks} package.较少使用yield这个方法。*/public static native void yield();
getId():返回该线程的id;
getName():返回该线程的名字;
getPriority():返回该线程的优先级;
interrupt():使该线程中断;
isInterrupted():返回该线程是否被中断;
isAlive():返回该线程是否处于活动状态;
isDaemon():返回该线程是否是守护线程;
setDaemon(boolean on):将该线程标记为守护线程或用户线程,如果不标记默认是非守护线程;
setName(String name):设置该线程的名字;
setPriority(int newPriority):改变该线程的优先级;
join():等待该线程终止;
join(long millis):等待该线程终止,至多等待多少毫秒数。