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 线程状态转换的更多相关文章

  1. Java线程状态转换

    前言:对于Java线程状态方面的知识点,笔者总感觉朦朦胧胧,趁着最近整理资料,将Java线程状态方面的知识点总结归纳,以便加深记忆. 1.Java线程状态值 在Thread类源码中通过枚举为线程定义了 ...

  2. 浅谈 Java线程状态转换及控制

    线程的状态(系统层面) 一个线程被创建后就进入了线程的生命周期.在线程的生命周期中,共包括新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死亡(Dead)这五 ...

  3. 一文读懂Java线程状态转换

    前言 本文描述Java线程线程状态及状态转换,不会涉及过多理论,主要以代码示例说明线程状态如何转换. 基础知识 1. 线程状态 Thread源码中的状态说明: 线程可以有6种状态: New(新建) R ...

  4. JAVA 线程状态转换图示及说明

    线程状态类型 新建状态(New):新创建了一个线程对象. 就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获取C ...

  5. Java中的线程状态转换和线程控制常用方法

    Java 中的线程状态转换: [注]:不是 start 之后就立刻开始执行, 只是就绪了(CPU 可能正在运行其他的线程). [注]:只有被 CPU 调度之后,线程才开始执行, 当 CPU 分配给你的 ...

  6. Java线程状态及 wait、sleep、join、interrupt、yield等的区别

    Java中的线程状态(详见Java线程状态及转换-MarchOn): wait:Object类的实例方法,释放CPU执行权,进入等待状态,直到  被中断.被拥有该对象锁的线程唤醒(notify或not ...

  7. Java线程状态和关闭线程的正确姿势

    1.线程状态及切换 Java中的线程有六种状态,使用线程Thread内的枚举类来实现,如下,我对每个状态都进行了一定的解释. public enum State { /** 表示一个线程还没启用(即未 ...

  8. Java线程状态Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释

    一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...

  9. 四十二、Linux 线程——线程同步之条件变量之线程状态转换

    42.1 线程状态转换 42.1.1 状态转换图 42.1.2 一个线程计算,多个线程获取的案例 #include <stdio.h> #include <stdlib.h> ...

随机推荐

  1. hadoop2.7.1单机和伪集群的搭建-0

    内容中包含 base64string 图片造成字符过多,拒绝显示

  2. 【Python】多线程2

    threading模块 import time import random import threading class Inclass: def __init__(self): print 'Inc ...

  3. 超全面的JavaWeb笔记day17<JDBC>

    1.JDBC的原理 是由JavaEE提供的连接数据库的规范 需要由各大数据库的厂商提供对JDBC的实现类 2.四大核心类 3.四大参数 driverClassName url username pas ...

  4. Android Tab切换

    ViewPager+FragmentStatePagerAdapter 页面切换案例详解 http://blog.csdn.net/u010203181/article/details/4462963 ...

  5. ionic安装及测试

    官方教程: http://ionicframework.com/getting-started/ 官方教程写得比较简单,简单来说就是 1)安装nodejs(安装方法:http://www.cnblog ...

  6. redis的初认识

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

  7. linux上如何快速删除一个目录

    在linux中删除一个目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可解决.直接rm就可以了,不过要加两个参数-rf 即:rm -rf   ...

  8. iOS设计模式之类族(class cluster)

    类族模式在UIKit(user interface framework)使用的范围已经远远超过我们的想象,比如,UIButton,NSArray,NSString,NSNumber等, 例如NSNum ...

  9. iOS-代码修改Info.plist文件

    解决办法: 1.首先系统的Info.Plist文件是只读文件 并不能 写入.目前我个人是没有办法存入,官方属性 可以看到是readOnly 2.那么我们 就想代码修改Info.Plist文件怎么办呢, ...

  10. 《转载》POI导出excel日期格式

    参考帖子: [1]http://www.ithao123.cn/content-2028409.html [2]http://javacrazyer.iteye.com/blog/894850 再读本 ...