Life Cycle of Thread – Understanding Thread States in Java
Life Cycle of Thread – Understanding Thread States in Java 深入理解java线程生命周期.
Understanding Life Cycle of Thread and Thread States are very important when you are working with Threads and programming for multi-threaded environment.
理解线程的生命周期很重要滴,当你在你的程序中使用线程或者多线程的时候.
As we learned in 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类,怎样开启线程为我们工作那?很简单,第一步创建线程对象(就是你继承或者是实现的线程类对象)然后调用线程方法start(),然后线程内部的run方法就会被调用,然后就算是开启了一个线程。
线程状态
Thread States
Below diagram shows different states of thread in java, note that 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中创建线程然后开启线程,至于线程状态是怎样阻塞还是可运行这些依赖于操作系统自身是如何调度的,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,此时线程并没有处于激活状态,而是仅仅是java线程中内部的一个状态.
Runnable
When we call start() function on Thread object, it’s state is changed to Runnable and 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 it depends on the OS implementation of thread scheduler.
当线程对象调用方法start()的时候,他的状态将会切换为Runnable并且线程调度器去调度并且完成所需要完成的工作,完成所有的工作之后,洗个状态的切换时立刻进入Running,还是继续保持Runnable状态这些依赖操作系统是如何进行调度。跟咱们java代码无关。(大家也许注意了这里有说道in runnalbe thread pool,其实就是这里存储Runnnable状态的池子,在多线程中在这个状态的可能不止你一个未来方便管理这里就引入了这runnable thread pool)。
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 and 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.
当线程进入此状态(Running),表明当前线程已经获得了cpu执行权,(线程调度是分片执行的,也是执行一个时间片之后,重新分配cpu资源,不然cpu一直执行一个线程,那样如果这个线程很耗时,那不就惨了,界面简直卡死)很简单就是从runnable thread pool咯一个线程粗来,然后修改状态,同时执行改线程。这里也说了虽然你处于这个状态(Running)但是这里很可能给你打回原形(Runnable状态),还有就是死了的状态(Dead),在或着阻塞状态(Block)这里依赖时间片内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.
你可以使用join方法让当前线程阻塞,一直到调用join的线程执行结束。或者是当前线程需要别的别的线程的资源(如:生产者和消费者消费者)这时候当前现场的状态会被修改为Waiting状态。当等待时间过来之后,此时状态会被修改为Runnable状态,进入in runnable thread pool,等待cpu的执行。
Dead
Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive.
Above are the different states of thread and it’s good to know them and how thread changes it’s state.
一旦线程执行结束此时状态就会变成Dead状态,该状态是最终状态不可修改。
上述的不同线程状态,你需要知道是怎么切换的,有助于你更好的理解线程。
翻译不当之处,愿君谅解!!!
Life Cycle of Thread – Understanding Thread States in Java的更多相关文章
- windbg学习---!thread和.thread
!thread扩展显示目标系统中线程包括ETHREAD块在内的摘要信息.该命令只能在内核模式调试下使用 !thread [-p] [-t] [Address [Flags]] -p 显示拥有该线程的进 ...
- 被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit
最近学习多线程的知识,看到API里说这些方法被废弃了,就查了一下原因 Thread.stop 这个方法会解除被加锁的对象的锁,因而可能造成这些对象处于不一致的状态,而且这个方法造成的ThreadDea ...
- Java Thread.interrupt 害人! 中断JAVA线程(zz)
http://www.blogjava.net/jinfeng_wang/archive/2012/04/22/196477.html#376322 ————————————————————————— ...
- Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?
Thread.stop, Thread.suspend, Thread.resume被标记为废弃的方法.在查看JDK的文档时,提到了下面的参考文章,先是英文版,接着是中文翻译. Why is Thre ...
- Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...
- java中的线程(1):如何正确停止线程Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?
转自 : http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html 1.Why is Th ...
- 【C# 线程】 volatile 关键字和Volatile类、Thread.VolatileRead|Thread.VolatileWrite 详细 完整
overview 同步基元分为用户模式和内核模式 用户模式:Iterlocked.Exchange(互锁).SpinLocked(自旋锁).易变构造(volatile关键字.volatile类.Thr ...
- Thread系列——Thread.Sleep(0)
转载自:http://www.cnblogs.com/ATually/archive/2010/10/21/1857261.html 线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执 ...
- 【Python@Thread】thread模块
一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行. Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的 ...
随机推荐
- Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- js获取?后面具体参数的值
function getURLParam(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...
- 李洪强iOS经典面试题141-报错警告调试
李洪强iOS经典面试题141-报错警告调试 报错警告调试 你在实际开发中,有哪些手机架构与性能调试经验 刚接手公司的旧项目时,模块特别多,而且几乎所有的代码都写在控制器里面,比如UI控件代码.网络 ...
- [DEMO] 互联网广告RTB机制简介
前言: 传统的互联网广告一般都是大流量网站在页面中留出一定空位,某些推广商家通过买位的方式来展示自己的广告. 我们这里引入一个案例:假设大访问量网站为博客园,想要广告推广的公司为阿里云平台. (场景为 ...
- word-wrap: break-word;和word-break: break-all;的区别
详细查看以下链接.(转载自张鑫旭大神空间) http://www.zhangxinxu.com/wordpress/2015/11/diff-word-break-break-all-word-wra ...
- php文件写入PHP_EOL与FILE_APPEND
PHP_EOL 换行符 unix系列用 \n windows系列用 \r\n mac用 \r PHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 FILE_APPEND 用于文本追加 ...
- MongoDB数据库的操作,增删改查
在student集合中插入一些数据 db.student.insert({ "学号":10010, "姓名":"德莱文", "年龄 ...
- PHP 生成PDF
一个项目中需要用到网页生成PDF,就是将整个网页生成一个PDF文件, 以前也用过HTML2PDF,只能生成一些简单的HTML代码,复杂的HTML + css 生成的效果惨不忍睹, 百度了一下,发现有个 ...
- 带你玩转JavaWeb开发之三 -JS插件实战开发
前提:需要掌握的知识点 填写HTML代码 Element元素中有一个innerHTML属性,这个属性可以填写一段html代码 innerHTML = "<font ...
- Elasticsearch判断多列存在、bool条件组合查询示例
and符号判断多列存在:{ "filter": { "and": [ { "exists": { ...