java中线程的生命周期_Java中的线程生命周期– Java中的线程状态

java中线程的生命周期

Understanding Thread Life Cycle in Java and Thread States are very important when you are working with Threads and programming for multithreaded environment.

使用线程和针对多线程环境进行编程时,了解Java线程状态下的 线程生命周期非常重要。

From our last tutorial, we can create a java thread class by implementing Runnable interface or by extending Thread class, but to start a java thread, we first have to create the Thread object and call it’s start() method to execute run() method as a thread.

从我们过去的教程中,我们可以创建一个Java线程通过实现Runnable接口或者扩展Thread类的类,但启动Java线程,我们首先必须创建Thread对象并调用它的start()方法来执行run()方法作为线程。

Java中的线程生命周期 (Thread Life Cycle in Java)

Below diagram shows different states of thread life cycle in java. We can create a thread in java and start it but how the thread states change from Runnable to Running to Blocked depends on the OS implementation of thread scheduler and java doesn’t have full control on that.

下图显示了Java中线程生命周期的不同状态。 我们可以在Java中创建一个线程并启动它,但是线程状态如何从Runnable更改为Running到Blocked取决于线程调度程序的OS实现,而Java对此没有完全控制。

(New)

When we create a new Thread object using new operator, thread state is New Thread. At this point, thread is not alive and it’s a state internal to Java programming.

当我们使用new运算符创建新的Thread对象时,线程状态为New Thread。 在这一点上,线程还没有激活,它是Java编程内部的一种状态。

可运行 (Runnable)

When we call start() function on Thread object, it’s state is changed to Runnable. The control is given to Thread scheduler to finish it’s execution. Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the OS implementation of thread scheduler.

当我们在Thread对象上调用start()函数时,它的状态更改为Runnable。 将控制权交给线程调度程序以完成其执行。 是立即运行此线程还是在运行之前将其保留在可运行线程池中,取决于线程调度程序的OS实现。

跑步 (Running)

When thread is executing, it’s state is changed to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running. Then CPU starts executing this thread. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources.

线程执行时,其状态更改为正在运行。 线程调度程序从可运行线程池中选择一个线程,并将其状态更改为“正在运行”。 然后,CPU开始执行该线程。 线程可以将时间从运行状态更改为Runnable,Dead或Blocked,这取决于时间切片,run()方法的线程完成或等待某些资源。

封锁/等待 (Blocked/Waiting)

A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to available. For example producer consumer problem or waiter notifier implementation or IO resources, then it’s state is changed to Waiting. Once the thread wait state is over, it’s state is changed to Runnable and it’s moved back to runnable thread pool.

一个线程可以等待其他线程使用线程连接完成,也可以等待一些可用资源。 例如生产者消费者问题服务员通知程序实现或IO资源,则其状态更改为等待。 线程等待状态结束后,其状态将更改为Runnable,并将其移回可运行线程池。

(Dead)

Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive.

线程执行完毕后,其状态将更改为Dead(死),并且认为该状态不活跃。

Above are the different states of thread. It’s good to know them and how thread changes it’s state. That’s all for thread life cycle in java.

以上是线程的不同状态 。 了解它们以及线程如何更改其状态非常好。 这就是Java中线程生命周期的全部内容。

翻译自: https://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java

java中线程的生命周期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值