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

 

posted @ 2018-09-17 17:16  两条闲鱼  阅读(161)  评论(0编辑  收藏  举报