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的更多相关文章

  1. windbg学习---!thread和.thread

    !thread扩展显示目标系统中线程包括ETHREAD块在内的摘要信息.该命令只能在内核模式调试下使用 !thread [-p] [-t] [Address [Flags]] -p 显示拥有该线程的进 ...

  2. 被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit

    最近学习多线程的知识,看到API里说这些方法被废弃了,就查了一下原因 Thread.stop 这个方法会解除被加锁的对象的锁,因而可能造成这些对象处于不一致的状态,而且这个方法造成的ThreadDea ...

  3. Java Thread.interrupt 害人! 中断JAVA线程(zz)

    http://www.blogjava.net/jinfeng_wang/archive/2012/04/22/196477.html#376322 ————————————————————————— ...

  4. Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?

    Thread.stop, Thread.suspend, Thread.resume被标记为废弃的方法.在查看JDK的文档时,提到了下面的参考文章,先是英文版,接着是中文翻译. Why is Thre ...

  5. Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space

    在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...

  6. 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 ...

  7. 【C# 线程】 volatile 关键字和Volatile类、Thread.VolatileRead|Thread.VolatileWrite 详细 完整

    overview 同步基元分为用户模式和内核模式 用户模式:Iterlocked.Exchange(互锁).SpinLocked(自旋锁).易变构造(volatile关键字.volatile类.Thr ...

  8. Thread系列——Thread.Sleep(0)

    转载自:http://www.cnblogs.com/ATually/archive/2010/10/21/1857261.html 线程这一概念,可以理解成进程中的一个小单元.这个单元是一个独立的执 ...

  9. 【Python@Thread】thread模块

    一.关于Python多线程 Python解释器中可以同时运行多个线程,但是再任意时刻只能有一个线程在解释器运行. Python虚拟机的访问是由全局解锁器(GIL)控制的,由GIL保证同时只有一个线程的 ...

随机推荐

  1. Jenkins + Ant + Git + Tomcat自动化部署

    环境linux下,大致的配置内容如下: 首先安装JDK配置环境变量等. 其次安装ANT配置ANT_HONE并把bin目录加入PATH中. 然后安装Git,并生成sshkey配置ssh 安装tomcat ...

  2. HTML5 标签audio添加网页背景音乐代码

    <head> <meta http-equiv="Content-Type" name="viewport" content="wi ...

  3. 2014亚马逊在线笔试题目及解决方案(MMChess问题)

    整体思路:关键是需要知道当前Steps数组中的全排列即可,而且需要不重复的全排列.即关键在于非递归的全排列实现即可~ 其实直接利用STL中的next_permutation算法的,这里我又自己实现了一 ...

  4. MySQL导出数据

    1.MySQL导出数据库 只导出数据库结构:选中数据库-->右键—>数据传输—>高级—>取消勾选记录选项.

  5. 解决Windows Server2008R2中导入Excel不能使用Jet 4.0

    操作系统使用了Windows Server2008R2(Windows Server2008R2是64位系统,同时配置IIS7.0 原读取Excel文件代码为: string strConn = st ...

  6. NOSQL场景梳理

    Redis 场景:缓存,Session,消息发布订阅,产品属性分析,订单购买等强事务,计数等   Memcached 场景:读密集,写一般的缓存,Session   MongoDB 场景:数据显示,查 ...

  7. Node.js用ES6原生Promise对异步函数进行封装

    Promise的概念 Promise 对象用于异步(asynchronous)计算..一个Promise对象代表着一个还未完成,但预期将来会完成的操作. Promise的几种状态: pending:初 ...

  8. MySQL查询语句(select)详解(2)

    7.子查询 当进行查询的时候,需要的条件是另外一个select语句的结果,这时候就要用到子查询 用于子查询的主要关键字有:in,not in,=,!=,exists,not exists等. 以下两张 ...

  9. SQLServer注入技巧

    一.对于SA权限的用户执行命令,如何获取更快捷的获取结果? 有显示位 无显示位 其实这里的关键并不是有无显示位.exec master..xp_cmdshell 'systeminfo'生成的数据写进 ...

  10. 改变spring-servlet.xml名字和默认位置

    如何我们在web.xml定义: <servlet> <servlet-name>spring</servlet-name> <servlet-class> ...