Java concurrent Programming thread state, synchronized

Source: Internet
Author: User

The following content is mainly summarized from the "Java Multithreaded Programming core technology", the update is not scheduled to supplement.

First, the state of the thread

In Java, there are 6 types of threads in the state: NEW, RUNNABLE, BLOCKED, waiting, timed_waiting, TERMINATED. The relationships between the States can be expressed as:

Ii. 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 en D "); 5     }

Call the Start () method to start the thread t,t the state of the thread is executed concurrently from the new-and runnable,t thread and main thread.

If you change T.start () to T.run (), then it is normal to call the method, synchronous execution, System.out.println ("main end") statement must wait until the T.run () method is completed before execution.

Note: the T.run () method does not change the state of the thread T, which means that the thread does not start.

2. The difference between object.wait () and Thread.Sleep ()

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}

The wait () method is primarily used in the synchronized synchronization method or in a synchronous block, meaning that the lock must be acquired before calling Object.wait (), and the lock is released after the Wait () method is called, and the thread enters the waiting state. Common usage is shown above. If there are other threads that call the Object.notify () or Object.notifyall () method, the thread must acquire the obj lock again before it can continue executing the statement after obj.wait (), which is printing "TestMethod end". Obj.wait (timeout) method is similar, you need 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 is timed_waiting from runnable, but does not release the lock.

3. Interrupt () method

When a thread calls the interrupt () method, it simply sets the interrupt state of the thread. This means that if the thread is in the runnable or blocked state, calling the interrupt () method does 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 throws an exception, terminating the thread.

And then I found it wrong. See the following 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 {ten         lock.unlock ();     }12}

The condition.awaituninterruptibly () method does not need to catch the interruptedexception exception, meaning that if the thread is waiting by calling awaituninterruptibly so that the thread state is is not interrupted by calling the interrupt () method. The actual test, the thread state does not respond to the interrupt method, can only wake the thread by Condition.singal or Singalall.

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

Three, synchronized key words

1, synchronized (object): Synchronous 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.

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

Java concurrent Programming thread state, synchronized

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.