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保证同时只有一个线程的 ...
随机推荐
- iOS 消息推送(APNs) 傻瓜式教程
也可以去我的简书页面查看这篇文章 首先: 1.做iOS消息推送需要真机测试 2.做iOS消息推送需要有付费的开发者账号 是否继续看帖? 先学习一下相关的知识吧! 因为中途可能会遇到一些问题,这篇文章或 ...
- [资料分享]Python视频教程(基础篇、进阶篇、项目篇)
Python是一种开放源代码的脚本编程语言,这种脚本语言特别强调开发速度和代码的清晰程度.它可以用来开发各种程序,从简单的脚本任务到复杂的.面向对象的应用程序都有大显身手的地方.Python还被当作一 ...
- 基于华清远见STM32f051的 IIC从模式实现方法
作者:卢老师,华清远见嵌入式学院讲师. 在大多情况下,我们使用MCU控制传感器,节点以及相关从设备,但在较为复杂的系统中,有时候也会使用MCU做为从设备. 下面是关于stm32f051的从模式实现方法 ...
- Mysql创建新用户方法
1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...
- JavaScript字符串的操作-课堂笔记
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浅谈iOS触摸事件理解
iOS的触摸事件个人总结,分为两步: 第一步:是找到哪个视图上触摸 第二步:分析由谁去响应(响应者连) 1.寻找被触摸的视图原理如下图 hitText:withEvent:的方法处理流程: 首先会在当 ...
- shujuk
数据库管理的目标:在适当的时候以适当的形式向适当的人提供适当的数据. 2. 数据管理的内容:组织业务的管理(学生的信息) :技术的管理(数据库的建立等) 3. 数据库管理的发展阶段:人工,文件,数据库 ...
- jsp 页面标签 积累
http://www.cnblogs.com/xiadongqing/p/5232592.html <%@ taglib %>引入标签库 ========================= ...
- Eclipse修改编码格式
♣修改工作空间默认编码 ♣修改文件的编码 ♣修改某文件类型的编码 ♣修改JSP文件类型的编码 1.修改工作空间默认编码 window -> preferences -> General ...
- IBM 3090 with Vector Facility
COMPUTER OR GANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION