什么是Java线程的生命周期? [英] What is the LifeCycle of Thread in Java?

查看:119
本文介绍了什么是Java线程的生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,当我们创建线程对象

In java when we create an object of thread

Thread t1 = new Thread(Runnable object);
t1.start();

线程t1的生命周期有哪些不同阶段,并且在执行run()之后将成为t1的状态?

What are the different stages of lifecycle of thread t1 and after execution of run() will be the state of t1?

推荐答案

线程在其生命周期中经历了多个阶段.例如,线程是先生,启动,运行然后死亡的.下图显示了线程的完整生命周期.

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows the complete life cycle of a thread.

Java线程 上述步骤在这里进行了解释:

Java Thread Above-mentioned stages are explained here:

New:新线程以新状态开始其生命周期.它将保持这种状态,直到程序启动线程为止.也称为出生线程.

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable:启动新出生的线程后,该线程变为可运行的.处于这种状态的线程被视为正在执行其任务.

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting:有时,一个线程在等待另一个线程执行任务时会转换为等待状态,仅当另一个线程发出信号等待该线程继续执行时,该线程才转换回可运行状态.

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed waiting:可运行线程可以在指定的时间间隔内进入定时等待状态.当该时间间隔到期或发生等待事件时,处于此状态的线程将转换回可运行状态.

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated ( Dead ):可运行线程在完成其任务或以其他方式终止时进入终止状态.

Terminated ( Dead ): A runnable thread enters the terminated state when it completes its task or otherwise terminates.

来源: http://www.tutorialspoint.com/java/java_multithreading.htm

这篇关于什么是Java线程的生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆