Java:多线程基础(二)-线程生命周期
创始人
2024-02-27 01:07:54
0

目录

线程生命周期

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:*

    *
  • {@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.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil会进入此状态。*/TIMED_WAITING,/*** Thread state for a terminated thread.* The thread has completed execution.死亡状态:线程体run()方法已经被执行完毕。*/TERMINATED; }

Thread类的常用方法

        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):等待该线程终止,至多等待多少毫秒数。

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...