Java concurrency programming note blocking and interrupts

Source: Internet
Author: User

>> state transitions for threads

The state transitions of threads are the basis of thread control, and the following image shows the state transitions of threads very visually:

state transitions between threads:
1. NEW: A new Thread object has been created.
2. Operational (runnable): After the thread object is created, other threads (such as the main thread) call the object's start () method. The thread in this state is in a pool of running threads, waiting for the thread to be dispatched to get the CPU right.
3. Run (running): The Running State (runnable) thread obtains the CPU time slice (TimeSlice) and executes the program code.
4. Block: Blocking state refers to a thread that has given up the CPU for some reason, or let the CPU TimeSlice, temporarily stop running. Until the thread enters the operational (runnable) state, there is a chance to get the CPU TimeSlice again to the run (running) state.
5. Death (dead): The thread Run (), main () method execution ends, or the run () method is exited due to an exception, the thread ends the life cycle. The thread of death cannot be resurrected again.

>> Thread-blocking scenarios

There are several cases of blocking:
(1) Waiting for blocking
The thread waits for a notification (notify), the running state of the thread executes the o.wait () method, and the JVM puts the thread in the wait queue (waitting queue).
(2) Synchronous blocking
When a thread running a state acquires a synchronization lock on an object, the JVM places the thread in the lock pool if the synchronization lock is occupied by another thread.
(3) Other obstruction
The running state of the thread execution Thread.Sleep (long ms) actively discards the system resources occupied;
The thread calls a blocking Io method, and the thread is blocked until the method returns;
Invoking the Join () method of another thread on the current thread also causes the current thread to block;
When the sleep () state time-out, join () waits for the thread to terminate or times out, or the I/O process is complete, the thread is re-transferred to the operational (runnable) state.

>> using the Join method

Thread. Join joins the specified thread to the current thread, and can merge two alternately executed threads into sequentially executed threads.

For example, thread A's join () method is called in Threads B, and thread B is not resumed until thread a finishes execution.
That is, thet.join () method blocks the thread that calls this method (calling thread)until the thread is finished and the thread continues.
Typically used in the main () main thread, waiting for other threads to finish before ending the main () main thread.

Join () method suspends the execution of the calling thread until the object called finishes its execution.

>>java interrupt Mechanism

The Java interrupt mechanism is a collaborative mechanism, meaning that the interrupt does not directly terminate the other thread, and the thread that needs to be interrupted handles the interrupt itself.

Each thread object has a Boolean identifier (not necessarily the field of the thread class, which is, in fact, not, which is ultimately done through the native method), and represents whether there is an interrupt request (the request can come from all threads, including the thread itself being interrupted).
The Java.lang.Thread class provides several ways to manipulate this interrupt state:

public static Boolean interrupted tests if the front thread has been interrupted. The interrupt state of the thread is purged by this method. In other words, if the method is called twice in a row, the second call will return False (except in the case where the current thread is interrupted again until the first call has cleared its break state and the second call has finished verifying the break state). public boolean isinterrupted () tests whether the thread has been interrupted. The interrupt state of the thread is not affected by the method. Break thread in public void interrupt ().

  

For example, when a thread T1 wants to break thread T2, it only needs to set the interrupt identifier of the thread T2 object to True in the threads T1, then thread 2 can choose to handle the interrupt request at the appropriate time, or even ignore the request, as if the thread had not been interrupted.
Thread T1 The interrupt state of the thread T2 by calling the interrupt method to True,t2 can call interrupted or isinterrupted at the appropriate time to detect the state and do the appropriate processing.

>> interrupted usage scenarios

Interrupt usage scenarios typically include the following:
When you click the Cancel button in a desktop app;
An operation exceeds a certain execution time limit when it needs to be aborted;
Multiple threads do the same thing, as long as a thread succeeds when other threads can be canceled;
When one or more errors in a group of threads cause an entire group to fail to continue;
When an app or service needs to stop.

Java concurrency programming note blocking and interrupts

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.