Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-07-17:说一下线程的几种状态? #99

Open
MoJieBlog opened this issue Jul 17, 2019 · 13 comments
Open

2019-07-17:说一下线程的几种状态? #99

MoJieBlog opened this issue Jul 17, 2019 · 13 comments
Labels

Comments

@MoJieBlog
Copy link
Collaborator

No description provided.

@gabyallen
Copy link

new
runnable
blocked
waiting
time-waiting
terminated

@live17909
Copy link

 第一是创建状态。在生成线程对象,并没有调用该对象的start方法,这是线程处于创建状态。
  第二是就绪状态。当调用了线程对象的start方法之后,该线程就进入了就绪状态,但是此时线程调度程序还没有把该线程设置为当前线程,此时处于就绪状态。在线程运行之后,从等待或者睡眠中回来之后,也会处于就绪状态。
  第三是运行状态。线程调度程序将处于就绪状态的线程设置为当前线程,此时线程就进入了运行状态,开始运行run函数当中的代码。
  第四是阻塞状态。线程正在运行的时候,被暂停,通常是为了等待某个时间的发生(比如说某项资源就绪)之后再继续运行。sleep,suspend,wait等方法都可以导致线程阻塞。
  第五是死亡状态。如果一个线程的run方法执行结束或者调用stop方法后,该线程就会死亡。对于已经死亡的线程,无法再使用start方法令其进入就绪。

@yangfanggang
Copy link

1.初始(NEW) ,创建线程对象
2.运行(RUNNABLE),此时就绪且正在运行一起称为运行
3.阻塞(BLOCKED),线程阻塞
4.等待(WAITING),等待中断等操作
5.超时等待(TIMED_WAITING),可以指定时间返回,不一定需要操作
6.终止(TERMINATED),线程执行完毕

具体参考:线程状态及切换

@18361237136
Copy link

  1. 创建状态
  2. 运行状态
  3. 阻塞状态
  4. 等待状态
  5. 终止状态

@Moosphan Moosphan changed the title 2019-07-17:说一下线程的几种状态? 2019-07-17:说一下线程的几种状态? Jul 19, 2019
@ZsHush
Copy link

ZsHush commented Aug 9, 2019

网上大多说五种,但也有六种的说法,萌新表示很迷惑啊大佬们

@MrCodeSniper
Copy link

线程的状态

当new Thread新建线程时 线程为NEW 新建状态
接着线程会进入锁池竞争获取锁 如果成功获取锁会进入就绪状态
获取失败

进入就绪状态的线程 会等待CPU分配时间片 之后进入运行状态 执行任务

如果线程在运行中被调用了sleep睡眠 中断join(放弃CPU占用而停止运行的情况)都会进入阻塞状态加入到阻塞队列中 等待唤醒或者时间到
释放锁的进锁池重新获取
保留锁的进入就绪状态 等待CPU时间片
如果调用的是wait方法 线程会进入等待状态(需要外部唤醒notify) 加入等待池 唤醒之后再去锁池竞争锁

执行完毕后进入死亡状态

@feelschaotic
Copy link

网上大多说五种,但也有六种的说法,萌新表示很迷惑啊大佬们

到底有多少种状态?看下 Thread.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;
    }

@Jmengfei
Copy link

Jmengfei commented Mar 26, 2021

  • 创建状态 NEW 新创建线程, 但是还没有调用start方法
  • 运行状态 RUNNABLE, java中的就绪状态和运行状态都归结为RUNNABLE状态。运行态调用yield进入就绪状态。系统调度进入运行状态。
  • 阻塞状态 BLOCKED。阻塞于锁的状态
  • 等待状态 WAITTING。 调用wait或者join进入等待状态。
  • 阻塞超时状态 TIMED_WAITTING 。通过调用wait,join, sleep等都有可能进入此状态
  • 销毁状态。terminated 任务执行完成。

java中比较容易混淆的几个api

  • Thread.yield() : 当前线程不释放锁资源,由运行状态变为就绪状态,让OS再次选择线程。作用:让相同优先级的线程轮流执行,但并不保证一定会轮流执行。实际中无法保证yield()达到让步目的,因为让步的线程还有可能被线程调度程序再次选中。不会导致阻塞。该方法与sleep()类似,只是不能由用户指定暂停多长时间。
  • Thread.sleep(millis): 当前线程,当前线程进入TIMED_WAITING状态,但不释放对象锁,millis后线程自动苏醒进入就绪状态。作用:给其它线程执行机会的最佳方式
  • Thread.join()。当前线程调用其他线程t的join方法,当前线程进入WAITTING/TIMED_WAITTING 状态。不释放已持有的对象锁。等待线程t执行完成或者时间结束。可能会进入BLocked状态。基于wait实现
  • Object.wait():当前线程调用对象的wait方法,当前线程进入等待队列。释放当前对象锁
  • Object.notify(): 唤醒等待队列中的任意一个线程。jdk 1.8 唤醒的是头节点(等待时间最长的那个)
  • Object.notifyAll(): 唤醒等待队列中的全部线程。

@Liquidlkw
Copy link

网上大多说五种,但也有六种的说法,萌新表示很迷惑啊大佬们

6种是操作系统的分类和java的分发略有不同

@9527-tkx
Copy link

new
runnable
blocked
waitting
timed_waitting
terminated

@laohezi
Copy link

laohezi commented Apr 6, 2023

感觉 stackoverflow 上这个人解释的 blcoked 和waiting的区别比较清楚

A thread goes to wait state once it calls wait() on an Object. This is called Waiting State. Once a thread reaches waiting state, it will need to wait till some other thread calls notify() or notifyAll() on the object.

Once this thread is notified, it will not be runnable. It might be that other threads are also notified (using notifyAll()) or the first thread has not finished his work, so it is still blocked till it gets its chance. This is called Blocked State. A Blocked state will occur whenever a thread tries to acquire lock on object and some other thread is already holding the lock.

Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.

@luckilyyg
Copy link

luckilyyg commented Apr 6, 2023 via email

@Empty0Qc
Copy link

Empty0Qc commented Apr 6, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests