Java's multi-threading understanding

Source: Internet
Author: User

Thread's properties (1) The thread's state thread may be in one of several states during its life cycle:
    • New (new): The thread object has just been created;
    • Runnable (can run): After calling the Start method on the Thread object, the corresponding threads will enter the Runnable state, and if dispatched by the thread dispatcher, the thread will become the current running (Running) thread;
    • Blocked (blocked): If a piece of code is "locked" by thread A, then thread B attempts to execute this code, thread B will enter the Blocked state;
    • Waiting (wait): When a thread waits for another thread to notify the thread scheduler of a condition, it itself enters the waiting state;
    • Time Waiting (timed wait): The difference between a timed wait and a wait is that the thread waits only for a certain period, and no longer waits if the timeout is exceeded;
    • Terminated (terminated): The Run method of the thread is executed or an uncaught exception causes the Run method to terminate unexpectedly into the Terminated state.

If no special instructions are added later, we will use blocking state to refer to blocked, waiting, time waiting.

(2) Thread priority in Java, each thread has a priority, by default, the thread inherits its parent thread's priority. You can use the SetPriority method to change the priority of a thread.    Java defines three constants that describe thread precedence: max_priority, Norm_priority, min_priority. Each time the thread scheduler dispatches a new thread, it first chooses a higher-priority thread. Thread priority, however, is highly dependent on the operating system, and in some systems the Java virtual machine even ignores the priority of the thread. Therefore, we should not rely on the priority of the correctness of the program logic. The thread priority related APIs are as follows:
void SetPriority (// Sets the priority of a thread, you can use the system-provided three priority constants //To make the current thread in a concession state, so that when there are other threads that have precedence greater than or equal to this thread, Thread Scheduler calls that thread   

Thread class. Method:

1.sleep: Hibernate; This is a static method that lets the current thread go into hibernation ( but the thread does not release the acquired lock ), which is actually the time waiting state we mentioned above, after waking from hibernation, The thread will enter into the runnable state. The sleep method has two overloaded versions, with the following declarations:

 public static native void sleep (long millis)  Throws Interruptedexception; // Let the current thread hibernate Millis The specified number of milliseconds  Public static native void Sleep (long Millis, int Nanos) throws interruptedexception; //     

       2. Join Method , an instance method that calls the Join method on a thread object in the current thread, causes the current thread to stop running, and then runs the current thread after that thread has finished running. That is, the part of the current thread that has not been executed is "connected" to another thread, and the current thread runs after the other thread finishes running. The Join method has the following overloaded versions:

 public final  Synchronized void join () throws  interruptedexception public final synchronized void Join (long millis) throws Interruptedexception; public final synchronized void join (long Millis, int Nanos) throws interruptedexception;         

A parameterless join indicates that the current thread waits until another thread has finished running, in which case the current thread is in the wating state, and the parameter indicates that the current thread is waiting for the specified time, in which case the current thread is in the waiting state. when the current thread enters the time waiting or waiting state by calling the Join method, the acquired lock is freed. in fact, the join method internally invokes an instance method of the object class. Wait, for this method we will describe below.

Java's multi-threading understanding

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.