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> ...
随机推荐
- kafka学习之-配置详解
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- javaScript实现归并排序
归并排序是一个O(nlogn)的算法,其基本思想就是一个分治的策略,先进行划分,然后再进行合并,下面举个例子.有这样一组数据: {5,4,1,22,12,32,45,21} 如果对它进行归并排序的话, ...
- MFC中编辑框Edit Control添加“变量”后
- Navicat for Mysql 如何备份数据库
Navicat for Mysql 如何备份数据库 打开界面如下 打开自己的的数据库 点击需要备份的数据库名 未完!!! 文章来自:http://jingyan.baidu.com/article/f ...
- linq在获取部门层级树种的应用
public string GetNavigationsJson() { AjaxA_NAVIGATIONS ajaxnavigations = new AjaxA_NAVIGATIONS(); IL ...
- 交换a、b的值temp = a; a = b; b = temp;比a = a^b;b = a^b;a = a^b;快
先看代码,交换a.b的值十亿次 <span style="font-size:14px;"> int a=222; int b=111; int size = 1000 ...
- HDFS原理解析(总体架构,读写操作流程)
前言 HDFS 是一个能够面向大规模数据使用的,可进行扩展的文件存储与传递系统.是一种允许文件通过网络在多台主机上分享的文件系统,可让多机器上的多用户分享文件和 存储空间.让实际上是通过网络来访问文件 ...
- Python 扩展知识:编程习惯
1. 使用四个空格作为缩进而不是Tab键2. 函数名定义时第二个单词首字母大写,如 getNum,类名定义时所有单词首字母大写,如 GetNum
- 2017年要学习的三个CSS新特性
这是翻译的一篇文章,原文是:3 New CSS Features to Learn in 2017,翻译的不是很好,如有疑问欢迎指出. 新的一年,我们有一系列新的东西要学习.尽管CSS有很多新的特性, ...
- oracle 与mysql 的当前时间比较
select p.id,p.order_Num,p.image_url,p.url,p.image_topic, p.is_download, p.big_image_url, p.begin_tim ...