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

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状态

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值