Spin lock, blocking lock, reentrant lock, pessimistic lock, optimistic lock, read-write lock, bias, lightweight lock, heavyweight lock, lock expansion, Object Lock and Class lock

Source: Internet
Author: User
1. Spin Lock
A spin lock enables a thread to perform an empty loop without being suspended while not acquiring a lock, (that is, the so-called spin, which is the execution of the empty loop itself), and if the thread can acquire a lock after several empty loops, continue executing. If the thread is still unable to get the lock, it will be suspended.
With a spin lock, the probability of a thread being suspended is relatively reduced, and the consistency of thread execution is relatively enhanced. Therefore, the competition for those locks is not very intense, lock takes up a very short time concurrent thread, has certain positive significance, but for the lock competition is intense, the single thread lock occupies the very long time the concurrent program, the spin lock in spins waits, often resolutely cannot obtain the corresponding lock, not only wasted the CPU time, In the end it is unavoidable to be suspended operation, instead of wasting the resources of the system.
In JDK1.6, the Java Virtual machine provides-xx:+usespinning parameters to unlock the spin lock and uses the-xx:preblockspin parameter to set the number of spin locks to wait.
At the beginning of JDK1.7, the parameters of the spin lock are canceled, the virtual machine no longer supports the user-configured spin lock, the spin lock is always performed, and the number of spin locks is automatically adjusted by the virtual machine.

Problems that may arise:
1. Excessively occupy CPU time: If the current holder of the lock does not release the lock for a long time, then the waiting person will occupy the CPU time slice for a long time, resulting in a waste of CPU resources, so you can set a time when the lock holder exceeds this time without releasing the lock, and the waiting person discards the CPU time slice;
2. Deadlock problem: Imagine a thread that tries to acquire a spin lock two times in a row (for example, in a recursive program), the first time this thread obtains the lock, and when the second attempt to lock is detected that the lock is occupied (in fact it is occupied by itself), then the thread waits for itself to release the lock instead of continuing. This causes a deadlock. Therefore, a recursive procedure using a spin lock should follow the following principles: a recursive program must not call itself when holding a spin lock, nor can it attempt to acquire the same spin lock when recursive calls are made.

2. Blocking lock
Let the thread into the blocking state to wait, when the corresponding signal (wake-up, time), you can enter the ready state of the thread, ready to state all the threads, through competition, into the running state.
In Java, the ability to enter \ Exit, block state, or include blocking locks is available in the Synchronized keyword (where the weight lock), reentrantlock,object.wait () \notify ()

3, can be re-entry lock
A reentrant lock, also known as a recursive lock, refers to the code in which the inner recursive function acquires the lock, but is unaffected, after the same thread's outer function acquires the lock.
Reentrantlock and synchronized are reentrant locks in the Java environment
The following is a usage instance
[Java] View plain copy public class test implements runnable{           public synchronized void get () {            system.out.println (Thread.CurrentThread (). GetId ());            set ();       }           Public synchronized void set () {            System.out.println (Thread.CurrentThread (). GetId ());       }            @Override        public void run ()  {            get ();       }        public static void main (String[] args)  {     &nbsP;     test ss=new test ();            new thread (ss). Start ();           new  Thread (ss). Start ();           new thread (ss). Start ();        }  }      public class test  implements runnable {       reentrantlock lock = new  reentrantlock ();          public void get ()  {            lock.lock ();            system.out.println (Thread.CurrentThread (). GetId ());            set ();           lock.unlock ();     &Nbsp;  }          public void set ()  {            lock.lock ();            system.out.println (Thread.CurrentThread (). GetId ());            lock.unlock ();       }            @Override        public void run ()  {           get ();       }           public static void main (String[] args)  {           test ss = new test ();            new thread (ss). Start ();             New thread (ss). Start ();           new threa

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.