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自己可是管不了这些事情。

Life Cycle of Thread – Understanding Thread States in 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状态,该状态是最终状态不可修改。

上述的不同线程状态,你需要知道是怎么切换的,有助于你更好的理解线程。

翻译不当之处,愿君谅解!!!

上一篇:SPOJ HIGH Highways ——Matrix-Tree定理 高斯消元


下一篇:新手福利:Apache Spark入门攻略