Suspend (suspend) and thread blocking tool class Locksupport

Source: Internet
Author: User

Suspend (suspend) and thread blocking tool class Locksupport

It is generally not recommended to use suspend to suspend threads, because suspend does not release any lock resources while causing the thread to pause. If any other thread wants to access a lock that is taken up by it, it will be implicated, causing it to continue running normally. The resume operation is performed on the corresponding thread.

Also, if the resume operation unexpectedly executes before suspend, the suspended thread may have a difficult chance of being executed, and what is more serious is that the lock it occupies will not be freed, and therefore may cause the whole system to work abnormally, and, for a suspended thread, from its thread state, It's still runnable.

/** * @authorLuozhiyun on 2018/6/24. */ Public classBadsuspend { Public StaticObject U =NewObject ();StaticChangeobjectthread T1 =New Changeobjectthread("T1");StaticChangeobjectthread t2 =New Changeobjectthread("T2"); Public Static classChangeobjectthreadextendsthread{ Public Changeobjectthread(String name) {Super.SetName(name); }@Override         Public void Run() {synchronized(u) {System. out.println("in"+GetName()); Thread.CurrentThread().Suspend(); }        }    } Public Static void Main(string[] args)throwsinterruptedexception {t1.Start(); Thread.Sleep( -); T2.Start(); T1.Resume(); T2.Resume(); T1.Join(); T2.Join(); }}

After execution we may get the following output:

in t1in t2

This indicates that two threads have entered the critical section successively, but the program does not exit

Thread Blocking class: Locksupport

It can block threads at any point in the thread. Compared to Thread.Suspend (), it makes up for situations in which 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, nor does it throw a interruptedexception

Locksupport static Method Park () can block the current thread, similar to Parknanos ()/Parkuntil () and other methods. They have a time-limited wait.

/** * @authorLuozhiyun on 2018/6/24. */ Public classLocksupportdemo { Public StaticObject U =NewObject ();StaticChangeobjectthread T1 =New Changeobjectthread("T1");StaticChangeobjectthread t2 =New Changeobjectthread("T2"); Public Static classChangeobjectthreadextendsthread{ Public Changeobjectthread(String name) {Super.SetName(name); }@Override         Public void Run() {synchronized(u) {System. out.println("in"+GetName()); Locksupport.Park( This); }        }    } Public Static void Main(string[] args)throwsinterruptedexception {t1.Start(); Thread.Sleep( -); T2.Start(); Locksupport.Unpark(t1); Locksupport.Unpark(T2); T1.Join(); T2.Join(); }}

This code can end normally and will not cause the thread to hang permanently due to the park () method

This is because the Locksupport class uses a similar semaphore mechanism. It prepares a license for each thread, and if the license is available, the park () function returns immediately and consumes the license (that is, the license becomes unavailable). If the license is unavailable, it will block. and Unpark () Allows a license to become available (but unlike semaphores, the license cannot accumulate, you cannot have more than one license, it will always have only one)

This feature makes it possible for the next park () operation to return immediately, even if the Unpark () operation occurs before Park ()

At the same time, a thread in park () hangs does not give the state of a puzzling runnable like suspend (). It will give a waiting state very clearly, even if it is caused by park ().

Suspend (suspend) and thread blocking tool class Locksupport

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.