JAVA 线程状态转换
Thread类中State枚举定义:
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}

- sleep(long)
public class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"========="+Thread.currentThread().getState());
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试i类:
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(new MyThread());
t.start();
Thread.sleep(1000L);
System.out.println(t.getName()+">>>>>>>"+t.getState());
}
}
运行结果:
Thread-0=========RUNNABLE
Thread-0>>>>>>>TIMED_WAITING Process finished with exit code 0
- join()
public class MyThread implements Runnable {
private Thread parent;
public MyThread(Thread thread) {
parent = thread;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "=========" + Thread.currentThread().getState());
long startTime = System.currentTimeMillis();
while ((System.currentTimeMillis() - startTime) / 1000 <= 2) {
}
System.out.println(parent.getName() + ">>>>" + parent.getState());
}
}
测试代码:
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(new MyThread(Thread.currentThread()));
t.start();
Thread.sleep(1000L);
System.out.println(t.getName()+">>>>>>>"+t.getState());
System.out.println(t.getName()+".join...");
t.join();
System.out.println(t.getName()+">>>>>>>"+t.getState());
System.out.println(Thread.currentThread().getName()+">>>>>>>"+Thread.currentThread().getState());
}
}
运行结果:
Thread-0=========RUNNABLE
Thread-0>>>>>>>RUNNABLE
Thread-0.join...
main>>>>WAITING
Thread-0>>>>>>>TERMINATED
main>>>>>>>RUNNABLE
- wait()
public class TestService {
private static Object lock=new Object();
public void test() throws InterruptedException {
synchronized (lock) {
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getState());
lock.wait();
}
}
}
public class MyThread implements Runnable {
private TestService testService;
public MyThread(TestService testService) {
this.testService = testService;
}
@Override
public void run() {
try {
testService.test();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试代码:
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread t=new Thread(new MyThread(new TestService()));
t.start();
Thread.sleep(1000L);
System.out.println(t.getName()+":"+t.getState());
}
}
运行结果:
Thread-0:RUNNABLE
Thread-0:WAITING
JAVA 线程状态转换的更多相关文章
- Java线程状态转换
前言:对于Java线程状态方面的知识点,笔者总感觉朦朦胧胧,趁着最近整理资料,将Java线程状态方面的知识点总结归纳,以便加深记忆. 1.Java线程状态值 在Thread类源码中通过枚举为线程定义了 ...
- 浅谈 Java线程状态转换及控制
线程的状态(系统层面) 一个线程被创建后就进入了线程的生命周期.在线程的生命周期中,共包括新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead)这五 ...
- 一文读懂Java线程状态转换
前言 本文描述Java线程线程状态及状态转换,不会涉及过多理论,主要以代码示例说明线程状态如何转换. 基础知识 1. 线程状态 Thread源码中的状态说明: 线程可以有6种状态: New(新建) R ...
- JAVA 线程状态转换图示及说明
线程状态类型 新建状态(New):新创建了一个线程对象. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获取C ...
- Java中的线程状态转换和线程控制常用方法
Java 中的线程状态转换: [注]:不是 start 之后就立刻开始执行, 只是就绪了(CPU 可能正在运行其他的线程). [注]:只有被 CPU 调度之后,线程才开始执行, 当 CPU 分配给你的 ...
- Java线程状态及 wait、sleep、join、interrupt、yield等的区别
Java中的线程状态(详见Java线程状态及转换-MarchOn): wait:Object类的实例方法,释放CPU执行权,进入等待状态,直到 被中断.被拥有该对象锁的线程唤醒(notify或not ...
- Java线程状态和关闭线程的正确姿势
1.线程状态及切换 Java中的线程有六种状态,使用线程Thread内的枚举类来实现,如下,我对每个状态都进行了一定的解释. public enum State { /** 表示一个线程还没启用(即未 ...
- Java线程状态Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释
一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...
- 四十二、Linux 线程——线程同步之条件变量之线程状态转换
42.1 线程状态转换 42.1.1 状态转换图 42.1.2 一个线程计算,多个线程获取的案例 #include <stdio.h> #include <stdlib.h> ...
随机推荐
- 对ChemDraw Professional 16.0你了解多少
ChemDraw Professional 16.0组件为科学家提供了最新的科学智能应用程序组件,绘制化学结构图和分析生物路径图. ChemDraw Professional 16.0 ChemDr ...
- TFS 强制删除锁定文件(数据库)
TFS:TFS2010 VS:VS2012 OS:Windows2008 DB:Sqlserver2008 R2 我们在团队开发当中,版本控制是一个不可忽略的工具.我们团队使用的是TFS2010这个版 ...
- mysql数据库中,查看某个数据库下的表的存储类型都有哪些
需求描述: 在备份数据库的时候,使用mysqldump进行数据库的备份,如果库中仅仅有innodb存储引擎, 那么使用--single-transaction就可以,如果还有其他的存储引擎类型就要使用 ...
- [转]ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)
在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie ...
- oracle如何将am,pm时间字符串改为时间格式
问题: 解决办法: 1.param["OPT_DATE"] = DateTime.Parse(dt.Rows[0]["CREATED_ON"].ToString ...
- Cocos2d-x 3.0final 终结者系列教程10-画图节点Node中的Action
Action是作用在Node上的逻辑处理,比方让Node移动.旋转.缩放.变色.跳跃.翻转.透明等等.都有相相应的Action Action怎样在Node上使用 1. 定义Action对象 如 aut ...
- 我学cocos2d-x (三) Node:一切可视化对象的祖先
在cocos2d-x中一切可视化的对象都继承自Node(如文字(label).精灵(sprite).场景(scene).布局(layer)).这是一个纯虚类.主要负责决定元素显示的位置. 由导演(Di ...
- ios 判断GPS是否是在中国境内
博文转载至 http://blog.csdn.net/cuibo1123/article/details/45691631 ZCChinaLocation 基本思路是:把整个行政区域划分为几个小的矩形 ...
- 如何在Oculus官网下载OculusSetup.exe(当前时间20170720)
踩着免费的蓝灯FQ登录了Oculus官网,找了半天找不到哪里下载OculusSetup.exe,最后在最下面的支持中心找到了..... https://www.oculus.com/
- 【IOS6.0 自学瞎折腾】(四)Xib可视化编程
其实在Interface Builder中,要把xib中的控件与代码联系起来用鼠标拖拉连线是非常方便的一件事,有的教程写的非常复杂要先点这后点那的. 一:IBOutlet,IB说明是Interface ...