"Java Core technology"----multithreading

Source: Internet
Author: User

Api:

Java.lang.Thread

    • static void sleep (long Millis) sleeps for the given number of milliseconds.
    • The thread (Runnable target) constructs a new thread that invokes the run () method for the given target.
    • void Start () starts this thread and will raise the call to the run () method. This method will return immediately, and the new thread will run in parallel.
    • void Run () invokes the Run method of the associated runnable.
    • void interrupt () sends an interrupt request to the thread. The interrupt state of the thread is set to true. If the thread is currently blocked by a sleep call, then the interruptedexception exception is thrown.
    • The static Boolean interrupted () tests whether the thread that is executing this command is interrupted. This is a static method. This call has a side effect that resets the current interrupt state to false.
    • The Boolean isinterrupted () test thread is terminated. Unlike a static interrupt method, this call does not change the interrupt state of the thread.
    • The static thread CurrentThread () returns the Thread object that represents the current thread of execution.

Java.lang.Runnable

    • void run () must overwrite this method and provide the task instruction to be executed in this method.

One, in the disconnection process

You can call the interrupt () method to send an interrupt request. However, if the thread is blocked, the interrupt state cannot be detected. This is where the interruptedexception anomalies occur. When the interrupt method is called on a blocked thread (call sleep or wait), the blocking call is interrupted by the interruptedexception.

Making an interrupt request does not mean that the thread will terminate immediately, and sending the request is only to cause the thread to notice that the thread that is requesting the interrupt can decide how to handle the interrupt request. After some threads have processed the exception, they continue to execute without regard to interrupts. But more generally, the thread will simply interrupt as a request to terminate. such as the following code:

 Public voidrun () {Try {        ...         while(! Thread.CurrentThread (). isinterrupted () &&More work to check) {             Domore work ; }    } Catch(Interruptedexception ex) {//Thread was interrupted during sleep or wait;}finally {        //cleanup, if required;    }    //exiting the Run method terminates the thread}

If the thread calls the sleep method, isinterrupted detection is not necessary and useless. If the sleep method is called in the interrupt state, it does not hibernate. Instead, it clears the state and throws interruptedexception.

Two, thread state

    • New (newly created)
    • Runnable (can be run)
    • Blocked (blocked)
    • Waiting (Wait)
    • Timed Waiting (timed Wait)
    • Terminated (terminated)

1, new Thread creation

New Thread (R), when a thread is in the new state, the program has not started running code in the thread.

2, can run threads

Once the Start method is called, the thread is in the runnable state. A running thread may or may not be running.

Once a thread starts running, it does not have to remain running at all times. The preemptive dispatch system gives each running thread a time slice to perform the task, and when the time slice runs out, the operating system deprives the thread of its running power and gives another thread a chance to run.

3, blocked threads and waiting threads

When a thread is in a blocked or waiting state, it is temporarily inactive.

To enter a condition that is blocked or waiting:

    • When a thread attempts to acquire an internal object lock that is held by another thread, the thread goes into a blocking state. When all other threads release the lock, and the thread scheduler allows the thread to hold the lock, the thread becomes non-blocking.
    • When the thread waits for another thread to notify the scheduler of a condition, it enters the waiting state.
    • There are methods that have a timeout parameter, which causes the thread to enter a timed wait state. This status will be maintained until the timeout expires or the appropriate notification is received. These methods are: Thread.Sleep (), object.wait (), Thread.Join (), Lock.trylock and Condition.await ()

"Java Core technology"----multithreading

Related Article

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.