java thread status (transfer)

Reprinted from: http://www.cnblogs.com/everSeeker/p/5487093.html#3798351

 

First, the state of the thread

    In Java, the state of the thread has the following six categories: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED. The relationship between the states can be represented by the following diagram:

 

2. Introduction of common methods

1. The difference between thread.start() and thread.run()

1 public static void main(String[] args) {
2         Thread t = new Thread();
3         t.start();
4         System.out.println("main end");
5     }

Call the start() method to start the thread t, the state of the t thread will be from New -> Runnable, the t thread and the main main thread will be executed at the same time.

If t.start() is changed to t.run(), it is an ordinary calling method, which is executed synchronously. The System.out.println("main end") statement must wait for the t.run() method to be executed before it can be executed. .

Note: The t.run() method does not change the state of thread t, that is, the thread is not started.

 

2. The difference between object.wait() and thread.sleep()

copy code
copy code
1 private Object obj = new Object();
2 public void testMethod() throws InterruptedException {
3     synchronized (obj) {
4         obj.wait();
5         System.out.println("testMethod end");
6     }
7 }
copy code
copy code

The wait() method is mainly used in synchronized synchronization methods or synchronized blocks, which means that the lock must be acquired before calling object.wait(), and the lock is released after calling the wait() method, and the thread enters the waiting state. Common usage is shown above. If another thread calls the object.notify() or object.notifyAll() method, the thread must acquire the obj lock again, and then can continue to execute the statement after obj.wait(), that is, print "testMethod end". The obj.wait(timeout) method is similar, but also needs to release the lock first.

The wait() method is a method of the Object class, and the sleep(timeout) method is a method of the Thread class. The thread calls the sleep(timeout) method, and the state changes from runnable -> timed_waiting, but does not release the lock.

 

3. interrupt() method

    When a thread calls the interrupt() method, it just sets the thread's interrupt status. That is to say, if the thread is in the runnable or blocked state, calling the interrupt() method will not terminate the thread. So, I take it for granted that if the thread is in the waiting or timed_waiting state, calling the interrupt method will throw an exception, thereby terminating the thread.

    Then found it was wrong. See the following code:

copy code
copy code
1 private ReentrantLock lock = new ReentrantLock();
 2 private Condition condition = lock.newCondition();
 3 public void testMethod() {
 4     try {
 5         lock.lock();
 6         System.out.println("wait begin");
 7         condition.awaitUninterruptibly();
 8         System.out.println("wait end");
 9     } finally {
10         lock.unlock();
11     }
12 }
copy code
copy code

    The condition.awaitUninterruptibly() method does not need to catch the InterruptedException exception, which means that if the thread makes the thread state waiting by calling awaitUninterruptibly, it will not be interrupted by calling the interrupt() method. In the actual test, the thread state does not respond to the interrupt method, and the thread can be woken up only through condition.singal or singalAll.

    The actual test, wait(), wait(timeout), join(), sleep(timeout), await(), await(timeout) and other methods can be interrupted by the interrupt() method.

 

Three, synchronized keyword

1. synchronized(object): Synchronized method or code block, lock is an object.

2. synchronized(this): this refers to the current object.

3. For static methods, such as synchronized public static void testMethod(), the lock is the current Class class.

4. If the code throws an exception, the lock is automatically released.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705832&siteId=291194637