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> ...
随机推荐
- Unity资源解决方案之AssetBundle
1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material. ...
- 如何快捷地使用ChemBio 3D检查结构信息
ChemBio 3D是一款三维分子结构演示软件,能够轻松快捷地进行化学结构的制作和立体旋转.ChemBio 3D Ultra 14作为ChemBio 3D的最新版本可以更加快捷地制作化学结构.本教程将 ...
- linux下 安装mysql教程
安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...
- swift--使用UserDefaults来进行本地数据存储
UserDefaults适合轻量级的本地客户端存储,存储一个值,新值可以覆盖旧值,可以重复存储,也可以存储一次,然后直接从UserDefaults里面读取上次存储的信息,很方便,用的时候,宏定义下,直 ...
- java 字符编码问题
编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串.但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是 ...
- 我们复习.Net的这些日子里
今天是我们复习前面学习的第三天,也是在今天我们结束了复习.之前上学时间感觉知识全是生僻的,在生活中很少用到,因此喜欢重复的记忆,而现在学习的知识全是现在乃至未来,将是每天我们使用的,所以就感觉没必要重 ...
- Discuz! X2验证码的产生及验证
http://www.mcqyy.com/wenku/jiaocheng/jzjc/cjc/106729.html http://blog.sina.com.cn/s/blog_4acbd39c010 ...
- RxJava && Agera 从源码简要分析基本调用流程(2)
版权声明:本文由晋中望原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/124 来源:腾云阁 https://www.qclo ...
- NodeJS路由(server.js + router.js)
手动路由... server.js 创建http服务器,解析pathname,传递给router处理. var http = require("http"); var url = ...
- 【BZOJ4372】烁烁的游戏 动态树分治+线段树
[BZOJ4372]烁烁的游戏 Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠.题意:给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠.烁烁他每次会跳到一个节点u,把周围与他距 ...