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保证同时只有一个线程的 ...
随机推荐
- BZOJ4643 : 卡常大水题
将边按权值$A$从小到大排序,从小到大枚举$\max(A)$,然后双指针从大到小枚举$\max(B)$. 按权值$B$用大根堆维护所有已经加入的边,每次$\max(B)$减少时,不断取出权值$B$最大 ...
- SqlServer操作大全
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
- HttpPostedFile类的使用方法
HttpPostedFile注意事项: (1) HttpPostedFile提供对客户端已上载的单独文件的访问. (2) 对于客户端已上载的文件,最好先保存到服务器,再访问:而不是直接访问 ...
- windows快捷键集锦
输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是8090,首先找到它. 查看被占用端口对应的PID,输入命令:netstat -aon|findstr &q ...
- jdbc向各种数据库发送sql语句
1.有了JDBC,向各种关系数据发送SQL语句就是一件很容易的事.换言之,有了JDBC API,就不必为访问Sybase数据库专门写一个程序,为访问Oracle数据库又专门写一个程序,或为访问Info ...
- 如何使用Google Map API开发Android地图应用
两年前开发过的GoogleMap已经大变样,最近有项目要用到GoogleMap,重新来配置Android GoogleMap开发环境,还真是踩了不少坑. 一.下载Android SDK Manager ...
- JetBrains注册码计算(IntelliJ IDEA 15.0注册码激活)
安装前修改时间为2099年等很多年后的时间点,安装后选择免费试用30天,然后退出idea,把时间修改回来,这样就可以成功注册了 如果之前使用过授权服务器等激活方式,不弹出试用界面,请到C:\Users ...
- 【转】C#中将JSon数据转换成实体类,将实体类转换成Json
http://wo13145219.iteye.com/blog/2022667 http://json2csharp.chahuo.com/ using System; using System.C ...
- linq group by多个字段,返回多个字段.
直接上例子.var wflist = from u in db.TWelFare where u.fy_no == fy_no orderby u.we_no group u by new { wen ...
- 【Html 学习笔记】第二节——文本格式
上一节基本已经了解了一些html的基础,这一节主要学习html处理文本相关内容,直接看内容吧. 字体: 预格式文本:<pre> 地址:<address> 缩写:<abbr ...