Java:多线程基础(二)-线程生命周期

目录

线程生命周期

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:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * 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:
     * <ul>
     *   <li>{@link #sleep Thread.sleep}</li>
     *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
     *   <li>{@link #join(long) Thread.join} with timeout</li>
     *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
     *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
     * </ul>
        (有限)等待状态:等待指定的时长。调用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处理器资源,回到等待队列中,与其它线程一起参与新一轮的同步锁竞争。但是,调度器也可以忽略这个暗示。
     *
     * <p> 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。因此,它的使用应该与详细的配置分析和基准测试相结合,以确保它实际上具有预期的效果(即:较少使用此方法)。
     *
     * <p> 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):等待该线程终止,至多等待多少毫秒数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是席木木啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值