【问题标题】:Will context-switching happen when thread changes its state?当线程改变其状态时会发生上下文切换吗?
【发布时间】:2019-10-02 08:48:36
【问题描述】:

在java中,线程改变状态时会发生上下文切换吗?

我搜索了很多网站,但没有找到任何关于此的文章。

所以如果有人可以提供帮助将不胜感激。

像这样:

doSomething();
//when Thread.sleep() is called, thread state will change from runnable to timed_waiting, so will context switching happen?
Thread.sleep(1000);

【问题讨论】:

  • 是的。当前的大多数实现都会告诉操作系统线程正在休眠并且可以切换上下文。

标签: java multithreading concurrency context-switching thread-state


【解决方案1】:

是的,sleep() 触发上下文切换到TIMED_WAITING

见:https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.State.html#TIMED_WAITING

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    相关资源
    最近更新 更多