java8中线程的状态

java8中获取线程状态的方法是Thread.getState(),返回的是一个enum类型,具体定义如下:

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;
}
共有6个状态,下面是对这6个状态转换的简单总结。

1,NEW,Thread对象已经new出来了,还没有调用start方法。

2,RUNNABLE 调用了start()方法,线程进入可运行态,这里java没有区分线程正在执行还是在等待分配cpu时间片而在等待,统一被归为 RUNNABLE。

3,BLOCKED 阻塞态,通常与锁有关系,线程在等待持有一把锁相关的资源,比如进入同步块,获取ReentryLock等。

4,WAITING 等待状态,线程在等待另外一个线程来唤醒自己。比如另外的线程调用notify, notifyAll, 或者另外的线程终止(join方法会等待此条件)。

5,TIMED_WAITING 定时的等待,是对WAITING的一种细化,WAITING是无限期等待,而这里只会等待有限的时间。

6,TERMINATED 线程执行完任务,终止了。

从概念上来说,4、5两种状态可以合并来看,所以最终有5个不同的线程状态。

package com.ljl.org.test4; /** *@DEMO:Interview *@Author:jilongliang *@Date:2013-4-17 * * 分别使用Runnable接口和Thread类编程实 编写一应用程序创建两个线程一个线程打印输出1—1000之间所有的奇数(Odd Number) * 另外一个线程打印输出1-1000之间所有的偶数(Even Number)要求两个线程随机休眠一 段时间后 继续打印输出下一个数 * * 创建线程有两种方式: 1.实现Runnable接口 2.继承Thread类 * 实现方式和继承方式有啥区别? * 实现方式的好处:避免了单继承的局限性 在定义线程时. * 建议使用实现方式 * 区别: * 继承Thread:线程代码存放Thread子类run方法 实现 * Runnable:线程代码存放接口的子类的run方法 * wait释放资源,释放锁 * sleep释放资源,不释放锁 */ @SuppressWarnings("all") public class Thread1 { public static void main(String[] args) { //方法一 /* OddNumber js = new OddNumber(); js.start(); EvenNumber os = new EvenNumber(); os.start(); while (true) { if (js.i1 == 1000 || os.i2 == 1000) { System.exit(-1); } } */ //方法二 OddNum on=new OddNum(); EvenNum en=new EvenNum(); new Thread(on).start(); new Thread(en).start(); while (true) { if (on.i1 == 1000 || en.i2 == 1000) { System.exit(-1); } } } } /** * ============================继承Thread的线程=============================== */ class EvenNumber extends Thread { int i2; @Override public void run() { for (i2 = 1; i2 <= 1000; i2++) { if (i2 % 2 == 0) { System.out.println("偶數" + i2); } try { sleep((int) (Math.random() * 500) + 500); } catch (Exception e) { } } } } class OddNumber extends Thread { int i1; @Override public void run() { for (i1 = 1; i1 <= 1000; i1++) { if (i1 % 2 != 0) { System.out.println("奇數" + i1); } try { sleep((int) (Math.random() * 500) + 500); } catch (Exception e) { } } } } /** * ============================实现Runnable的线程=============================== */ @SuppressWarnings("all") class OddNum implements Runnable { int i1; @Override public void run() { for (i1 = 1; i1 <= 1000; i1++) { if (i1 % 2 != 0) { System.out.println("奇數" + i1); } try { new Thread().sleep((int) (Math.random() * 500)+500); } catch (Exception e) { } } } } @SuppressWarnings("all") class EvenNum implements Runnable { int i2; @Override public void run() { for (i2 = 1; i2 <= 1000; i2++) { if (i2 % 2 == 0) { System.out.println("偶數" + i2); } try { /**在指定的毫秒数内让当前正在执行的线程休眠 * Math.random()一个小于1的随机数乘于500+500,随眠时间不会超过1000毫秒 */ //new Thread().sleep((int) (Math.random() * 500)+500); new Thread().sleep(1000);//也可以指定特定的参数毫秒 } catch (Exception e) { } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值