Implementing a spin lock with the CAs method of the Java Atom variable

Source: Internet
Author: User

In order to prevent unscrupulous site crawler crawling articles, hereby identified, reproduced please indicate the source of the article. Laplacedemon/shijiaqi.

http://www.cnblogs.com/shijiaqi1066/p/5999610.html

Realize:

 PackageSjq.mylock;Importjava.util.concurrent.atomic.AtomicReference; Public classSpinLock {PrivateAtomicreference<thread> owner =NewAtomicreference<>();  Public voidLock () {Thread CurrentThread=Thread.CurrentThread ();  while(!owner.compareandset (NULL, CurrentThread)) {//owner = = NULL, then Compareandset returns True, otherwise false. //can't get the owner's thread, constantly in the dead loop        }    }         Public voidUnLock () {Owner.set (NULL); //It can also be written, too troublesome, not necessary        /*Thread cur = thread.currentthread ();         Owner.compareandset (cur, null); */    }    }

Test:

 PackageSjq.mylock;ImportJava.util.concurrent.CountDownLatch;Importorg.junit.Test; Public classTestspinlock {Final Static intThread_num = 100; Static intx = 0; @Test Public voidTestlock ()throwsinterruptedexception {countdownlatch latch=NewCountdownlatch (Thread_num); //LockSpinLock SpinLock =NewSpinLock ();  for(inti = 0; i < Thread_num; i++) {            //start a child thread            NewThread (() {                                //each thread loops multiple times, locks frequently, unlocks.                  for(intn = 0; n < 100; n++) {spinlock.lock (); X++;                Spinlock.unlock ();    } latch.countdown (); //the child thread notifies the main thread that the work is complete. }). Start ();    } latch.await (); //The main thread waits for all child threads to end. System.out.println (x); //Final Print Result: 10000, no thread unsafe exception occurred.     }}

In order to prevent unscrupulous site crawler crawling articles, hereby identified, reproduced please indicate the source of the article. Laplacedemon/shijiaqi.

http://www.cnblogs.com/shijiaqi1066/p/5999610.html

Implementing a spin lock with the CAs method of the Java Atom variable

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.