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

最新文章

  1. js cookie
  2. 多个Python环境的构建:基于virtualenv 包
  3. window.location 结构
  4. 配置安卓运行环境/安卓sdk
  5. GOCR.js – 使用 JS 识别出图片中的文本
  6. Winform开发框架的业务对象统一调用方式
  7. Android SDK Manager更新报错
  8. How do I place a group of functions or variables in a specific section?
  9. 动画原理——绘制正弦函数&amp;环绕运动&amp;椭圆运动
  10. 共享bean
  11. 后缀数组--可重叠的K次最长重复子串(POJ3261)
  12. MongoDB的全文检索(Text Search)功能
  13. 【Java EE】从零开始写项目【总结】
  14. 181102 Python环境搭建(安装Sublime Text3)
  15. CSS回顾(基础知识,元素,选择器,盒子,颜色)
  16. MQ消息队列在软件开发中的作中
  17. 【转】AlphaGo Zero 和强人工智能
  18. 再次安装fedora23的一些遗留问题的解决
  19. 日志插件 log4net 的配置和使用
  20. 关于QT_Creator不能在线调试问题

热门文章

  1. 【Raspberry Pi】openwrt 路由
  2. angular ajax的使用及controller与service分层
  3. EasyTouch5初步用法和其中的一个Bug
  4. MathType有哪些功能
  5. 定义的函数在main中调用时提示找不到标识符
  6. mysql数据库,什么是数据库的全备份?
  7. java程序后台报错java.net.SocketException: Too many open files
  8. Redis(三)-- 主从同步
  9. MySQL只有information_schema,test两个数据库
  10. C语言之顺序结构