Java thread status

Source: Internet
Author: User

Java thread status

A thread generally has multiple States. Java has the following states:
-NEW
-RUNNABLE
-BLOCKED
-WAITING
-TIMED_WAITING
-TERMINATED

For the meaning of each State, see the source code comment in Thread. java:

Public enum State {/*** Thread state for a thread which has not yet started. * The Thread status that has not been started. for example, if only one thread is defined, the strat () method */NEW,/*** Thread state for a runnable thread is not called. 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. * It is currently being executed in JVM, but it needs to wait for other resources such as processor resources. The status */RUNNABLE,/*** Thread st Ate 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 }. * The thread waits for the monitor lock status. The thread may wait for the synchronization block/method or call the Object. after the wait () * method, repeat the status of a synchronization block/method. */BLOCKED,/*** Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods :*
  • *
    • {@link Object#wait() Object.wait} with no timeout
    • *
      • {@link #join() Thread.join} with no timeout
      • *
        • {@link LockSupport#park() LockSupport.park}
        • *
        * *

        A thread in the waiting state is waiting for another thread to * perform a participant action. ** For example, a thread that has calledObject. wait ()* On an object is waiting for another thread to call *Object. Policy ()OrObject. policyall ()On * that object. A thread that has calledThread. join ()* Is waiting for a specified thread to terminate. * The thread waits. It may be because the call to Object. wait does not time out, and Thread. join does not time out, * LockSupport. park. * The thread waiting status is waiting for another thread to execute a specific operation. */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 :*

        • *
          • {@link #sleep Thread.sleep}
          • *
            • {@link Object#wait(long) Object.wait} with timeout
            • *
              • {@link #join(long) Thread.join} with timeout
              • *
                • {@link LockSupport#parkNanos LockSupport.parkNanos}
                • *
                  • {@link LockSupport#parkUntil LockSupport.parkUntil}
                  • *
                  * The thread waits for a specific time. Sleep is called, or an Object with timeout is called. wait, join with timeout, * parkNanos of LockSupport or parkUntil */TIMED_WAITING,/*** Thread state for a terminated thread. * The thread has completed execution. * end thread */TERMINATED ;}

                  You can write a program to test these statuses:

                      Thread thread1 = new Thread(new Runnable() {        @Override        public void run() {        System.out.println(thread 1);        }    });    System.out.println(state: +thread1.getState());

                  Output:

                  State: NEW

                      Thread thread1 = new Thread(new Runnable() {        @Override        public void run() {        System.out.println(thread 1);        }    });    System.out.println(state: +thread1.getState());    thread1.start();    System.out.println(state: +thread1.getState());

                  Output:

                  State: NEW
                  State: RUNNABLE
                  Thread 1

                  In the producer-consumer model, when the producer speed is not fast enough to consume items, the consumer will wait:

                      final Object lock = new Object();    Thread t1 = new Thread() {        @Override        public void run() {        int i = 0;        while (true) {            synchronized (lock) {                try {                    lock.wait();                } catch (InterruptedException e) {                }                    System.out.println(i++);                }            }        }    };    t1.start();    System.out.println(state:  + t1.getState());

                  Output:

                  State: WAITING

                      final Object lock = new Object();    Thread t1 = new Thread() {        @Override        public void run() {        int i = 0;        while (true) {            synchronized (lock) {            try {                lock.wait(20 * 1000L);            } catch (InterruptedException e) {            }            System.out.println(i++);            }        }        }    };    t1.start();    Thread.sleep(1000);    System.out.println(state:  + t1.getState());

                  Output:

                  State: TIMED_WAITING

                  public class BlockTest {    final Object lock = new Object();    public BlockTest() {    }    public void test(){    synchronized(lock){        while(true){        }    }    }    public void run() throws InterruptedException{    Runnable r = new Runnable() {        @Override        public void run() {        test();        }    };    Thread t1 = new Thread(r);    Thread t2 = new Thread(r);    t1.start();    Thread.sleep(1000);    t2.start();    System.out.println(thread1: +t1.getState());    System.out.println(thread2: +t2.getState());    }}

                  Output:

                  Thread1: RUNNABLE
                  Thread2: BLOCKED

                      Thread t1 = new Thread(new Runnable() {        @Override        public void run() {        System.out.println(thread 1 running);        }    });    t1.start();    Thread.sleep(1000);    System.out.println(thread1 :+t1.getState());

                  Output:

                  Thread 1 running
                  Thread1: TERMINATED

                   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.