Thread线程有哪几种状态,这篇文章带你深入了解!

Thread线程有哪几种状态,这也是面试中很多小伙伴都会被问到的知识点。Thread线程状态是应该掌握的基础知识。但是很多小伙伴如果没有总结过的话,面试过程可能会懵圈,这里我稍作整理,希望小伙伴们可以查漏补缺。

Thread源码中有定义State的枚举类,State枚举类中有明确定义线程的状态。源码如下。

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        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}.
         */
        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.
         */
        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>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

通过源码阅读,我们可以知道线程的状态分别是NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED。

我们分别对线程不同状态进行简单描述:

  1. NEW:尚未启动的线程的线程状态。
  2. RUNNABLE:可运行线程的线程状态。线程正在运行或者正在获取CPU时间片。
  3. BLOCKED:线程的线程状态被阻止,正在等待监视器锁。
  4. WAITING:等待线程的线程状态。调用某个线程的wait()、join()、 locksupport park()处于等待状态的线程。
  5. TIMED_WAITING:具有指定等待时间的等待线程的线程状态。调用某个线程的具有指定正等待时间的方法sleep(long)、wait(long)、join(long)、LockSupport Parknanos()、LockSupport ParkUntil()处于指定等待时间的等待线程的线程状态。
  6. TERMINATED:终止线程的线程状态。

我这边也整理了一份相关的线程状态转换图以供大家参考。

以上代供参考,如有不当之处,欢迎指出!!!

更多干货,欢迎大家关注和联系我。期待和大家一起更好的交流、探讨技术!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Python 中创建线程有以下几种方式: 1. 使用 `threading` 模块: 使用这种方式需要创建一个 `Thread` 类的实例, 然后调用它的 `start()` 方法来启动新线程。 2. 使用 `_thread` 模块: 这种方式与使用 `threading` 模块类似, 也需要调用 `start_new_thread()` 函数来启动新线程。 3. 使用多进程: Python 中的 `multiprocessing` 模块可以轻松地创建新的进程。 4. 使用其他第三方库: 例如 `gevent` 和 `concurrent.futures` 等库也可以用来创建线程。 ### 回答2: 在Java中,创建线程有以下几种方式: 1. 继承Thread类:继承Thread类并重写其run方法,然后实例化该类的对象,调用start方法启动线程。示例代码如下: ``` public class MyThread extends Thread { @Override public void run() { // 线程要执行的代码 } } // 创建并启动线程 MyThread thread = new MyThread(); thread.start(); ``` 2. 实现Runnable接口:实现Runnable接口,并实现其run方法,然后创建Thread对象并将Runnable实例作为参数传递给Thread对象,最后调用start方法启动线程。示例代码如下: ``` public class MyRunnable implements Runnable { @Override public void run() { // 线程要执行的代码 } } // 创建并启动线程 MyRunnable runnable = new MyRunnable(); Thread thread = new Thread(runnable); thread.start(); ``` 3. 使用Callable和Future:使用Callable接口代替Runnable接口,重写call方法,然后创建ExecutorService对象,通过submit方法提交Callable对象,最后得到Future对象并通过get方法获取结果。示例代码如下: ``` public class MyCallable implements Callable<Integer> { @Override public Integer call() throws Exception { // 线程要执行的代码 return result; } } // 创建线程池并提交Callable对象 ExecutorService executor = Executors.newFixedThreadPool(1); MyCallable callable = new MyCallable(); Future<Integer> future = executor.submit(callable); // 获取线程结果 Integer result = future.get(); // 关闭线程池 executor.shutdown(); ``` 通过以上几种方式,可以灵活地创建和启动线程,并执行相应的任务。 ### 回答3: 创建线程的方式有以下几种: 1. 使用继承Thread类:创建一个新的类,继承自Thread类,并重写run()方法,在run()方法中定义线程要执行的任务。然后实例化这个新类的对象,并调用对象的start()方法来启动线程。 2. 实现Runnable接口:创建一个新的类,实现Runnable接口,并实现其中的run()方法。然后实例化这个新类的对象,并将对象作为参数传递给Thread类的构造方法创建Thread对象。最后调用Thread对象的start()方法启动线程。 3. 使用Callable和Future接口:创建一个新的类,实现Callable接口,并实现其中的call()方法。然后利用ExecutorService线程池的submit()方法提交Callable对象,得到一个Future对象,通过Future对象的get()方法获取线程的返回值。 4. 使用线程池:通过ExecutorService线程池来管理线程的创建和执行。可以使用ThreadPoolExecutor类来自定义线程池的大小和其他相关参数。 这些方式可以根据实际需求选择不同的方法来创建线程。使用继承Thread类或实现Runnable接口比较简单,适合简单的线程任务。使用Callable和Future接口可以获取线程的返回值。使用线程池可以有效管理线程的创建和执行,提高系统资源的利用率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值