热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java等待io线程状态,Java多线程的六种状态

Java-多线程的六种状态多线程的六种状态引入——————————————————查看Thread类的相关源码:123456789101112131415161718

Java - 多线程的六种状态

多线程的六种状态引入

——————————————————

查看 Thread 类的相关源码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98/**

* A thread state. A thread can be in one of the following states:

*

*

{@link #NEW}

* A thread that has not yet started is in this state.

*

*

{@link #RUNNABLE}

* A thread executing in the Java virtual machine is in this state.

*

*

{@link #BLOCKED}

* A thread that is blocked waiting for a monitor lock

* is in this state.

*

*

{@link #WAITING}

* A thread that is waiting indefinitely for another thread to

* perform a particular action is in this state.

*

*

{@link #TIMED_WAITING}

* A thread that is waiting for another thread to perform an action

* for up to a specified waiting time is in this state.

*

*

{@link #TERMINATED}

* A thread that has exited is in this state.

*

*

*

*

* A thread can be in only one state at a given point in time.

* These states are virtual machine states which do not reflect

* any operating system thread states.

*

* @since 1.5

* @see #getState

*/

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:

*

*

{@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.

*/

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}

*

*/

TIMED_WAITING,

/**

* Thread state for a terminated thread.

* The thread has completed execution.

*/

TERMINATED;

}

由上面的相关源码可以看出,Java 的线程其实是分为六种状态的(操作系统一般分为三种),java.lang.Thread.State 枚举类中定义了六种线程的状态:NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED,可以调用线程Thread中的 getState() 方法获取当前线程的状态

线程状态详解

————————————

线程六种状态:

线程状态

解释

NEW

刚刚创建的线程,这种线程还没有开始执行

RUNNABLE

表示线程所需的一切资源都已经准备好了

BLOCKED

线程暂停执行,直到获得请求的锁

WAITING

进入一个无时间限制的等待状态

TIMED_WAITING

进入一个有时间限制的等待状态

TERMINATED

线程完成执行,结束

《Java并发编程艺术》中的线程状态图:

9f97a6a5e5398da828c3bc580e33b4cf.png

《实战Java高并发程序设计》线程状态示意图:

b6c9f6491c3088bb5942a4d2d67e03c0.png

一、新建状态 (NEW)

NEW状态表示刚刚创建的线程,这种线程还没有开始执行,等到线程的 start() 方法调用时,才表示线程开始执行

二、运行状态(RUNNABLE)

表示线程所需的一切资源都已经准备好了

就绪状态:

当线程对象调用了 start () 方法之后,线程处于就绪状态,就绪意味着该线程可以执行,但具体啥时候执行将取决于 JVM 里线程调度器的调度

注意:

在 start () 方法的源码注释中,规定了对 start () 方法的使用

1It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

不允许对一个线程多次使用start,线程执行完成之后,不能试图用start将其唤醒

其他状态 → 就绪状态

线程调用 start () ,新建状态转化为就绪状态

线程 sleep(long) 时间到,等待状态转化为就绪状态

阻塞式 IO 操作结果返回,线程变为就绪状态

其他线程调用 join() 方法,结束之后转化为就绪状态

线程对象拿到对象锁之后,也会进入就绪状态

运行状态(RUNNING):

处于就绪状态的线程获得了CPU之后,真正开始执行run()方法的线程执行体时,意味着该线程就已经处于运行状态,需要注意的是,对于单处理器,一个时刻只能有一个线程处于运行状态,对于抢占式策略的系统来说,系统会给每个线程一小段时间处理各自的任务,时间用完之后,系统负责夺回线程占用的资源,下一段时间里,系统会根据一定规则,再次进行调度

运行状态转变为就绪状态的情形:

线程失去处理器资源。多线程一般是分配时间段,而不是一直执行完成,调用yield()静态方法,暂时暂停当前线程,让系统的线程调度器重新调度一次,它自己完全有可能再次运行。

yieId () 方法的的源码注释

1A 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.

提示调度程序,当前线程愿意放弃当前对处理器的使用。这时,当前线程将会被置为就绪状态,和其他线程一样等待调度,这时候根据不同优先级决定的概率,当前线程完全有可能再次抢到处理器资源。

三、阻塞状态(BLOCKED)

线程暂停运行,直到获得请求的锁

以下场景线程将会阻塞:

线程等待进入synchronized同步方法

线程等待进入synchronized同步代码块

线程取得锁,就会从阻塞状态转变为就绪状态

四、等待状态(WAITING)

进入一个无时间限制的等待状态

运行 → 等待

当前线程运行过程中,其他线程调用join()方法,当前线程将会进入等待状态

当前线程对象调用wait()方法

等待 → 就绪

等待的线程被其他线程对象唤醒,notify () 和 notifyAll ()

五、超时等待状态(TIMED_WAITING)

进入一个有时间限制的等待状态

运行 → 超时等待

调用静态方法Thread.sleep(long)

线程对象调用wait(long)方法

其他线程调用指定时间的join(long)

sleep() 和 yield() 的不同之处:

sleep(long) 方法会使线程转入超时等待状态,时间到了之后才会转入就绪状态,而yield()方法不会将线程转入等待,而是强制线程进入就绪状态

使用 sleep(long) 方法需要处理异常,而 yield() 不用

超时等待 → 就绪

同样的,等待的线程被其他线程对象唤醒,notify() 和 notifyAll()

六、消亡状态(TERMINATED)

即线程的终止,表示线程已经执行完毕

消亡的原因

run()和call()线程执行体中顺利执行完毕,线程正常终止

线程抛出一个没有捕获的Exception或Error

需要注意的是:主线成和子线程互不影响,子线程并不会因为主线程结束就结束

注意:

从NEW状态出发后,线程就不能回到NEW状态,同理,处于TERMINATED状态的线程也不能再回到RUNNABLE状态



推荐阅读
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
author-avatar
少女24梦_276
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有