Java multithreading--thread blocking tool class Locksupport

Source: Internet
Author: User
Tags object object

Brief Introduction

Locksupport is a handy and useful threading tool that can block threads anywhere in the thread.

Compared to Thread.Suspend (), it makes up for situations where the thread cannot continue because the resume () occurred before.

Compared to object.wait (), it does not need to obtain a lock on an object first, and does not throw a interruptedexception exception.

Locksupport static Method Park () can block the current thread, similar to Parknanos (), Parkuntil (), and other methods. They implement a time-limited wait, as shown in:

Example
1 ImportJava.util.concurrent.locks.LockSupport;2 /**3 * Created by Zhengbinmac on 2017/3/5.4  */5  Public classSuspendresumetest {6      Public StaticObject object =NewObject ();7     StaticTestthread T1 =NewTestthread ("Thread 1");8     StaticTestthread t2 =NewTestthread ("Thread 2");9      Public Static classTestthreadextendsthread{Ten          PublicTestthread (String name) { One             Super. SetName (name); A         } - @Override -          Public voidrun () { the             synchronized(object) { -System.out.println (GetName () + "Occupy: "); - //Thread.CurrentThread (). suspend (); - Locksupport.park (); +System.out.println (Thread.CurrentThread (). GetName () + "End of execution!" "); -             } +         } A     } at      Public Static voidMain (string[] args)throwsinterruptedexception { - T1.start (); -Thread.Sleep (200); - T2.start (); - //T1.resume (); - Locksupport.unpark (t1); in Locksupport.unpark (T2); - //T2.resume (); to T1.join (); + T2.join (); -     } the}

The code simply modifies the Java multithreading-expired suspend () Hang, resume () to continue the execution of instances in the thread, changing suspend () and resume () to park () and Unpark ().

After modification, the same problem: we still cannot guarantee that the Unpark () method takes place after Park (), but executes the code multiple times, and the discovery can always end normally, not because the order of the two methods causes the thread to hang permanently.

This is because it uses a mechanism similar to semaphore.

It prepares a license for each thread, and if the license is available, the park () method returns immediately and consumes the license (the license becomes unavailable) and, if the license is unavailable, is blocked.

The Unpark () method, in turn, makes a license available.

This feature makes it possible for the next park () operation to return immediately, even if the Unpark () operation occurs before Park (). This is why the above example code ended successfully.

At the same time, the park () suspended thread will not be as suspend () as the thread state Runnable,park () will explicitly give the waiting state, and the callout is caused by park (), as shown in:

This annotation makes it easier to analyze the problem, and you can use the park (object) method to set up a blocking object for the current thread, which will appear in a thread Dump.

Change 18 rows in the instance to:

Locksupport.park (this);

The Jstack output is:

Resources

[1] Practical Java High concurrency program design, 3.1.7-thread blocking tool class: Locksupport

[2] The Art of Java concurrent programming, 5.5-locksupport tools

Java multithreading--thread blocking tool class Locksupport

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.