Java Synchronous Lock method

Source: Internet
Author: User

Method One: Dynamic sync lock

class Implements runnable{    publicstaticint sum = 0;      Public synchronized void Add () {// Sync Lock for dynamic method for        (int i=0;i<5000;i++             {= sum + 1;        }    }      Public void run () {        add ();}    }

The dynamic Sync lock applies to the Runnable class where the thread class is not applicable, because its lock object is the current instance object, and a thread class can only run on a single threads, each thread is a different object, so the synchronization lock will not work.

Method Two: Static synchronous lock

 class  demo_thread2 extends   thread{ public  static  int  sum = 0;  public  static  synchronized  void  Add () {// Span style= "color: #008000;" > is a static method  for  (int  i=0;i<5000;i+        +) {sum  = sum + 1 public  void   run () {add (); }}

A static synchronization lock can be used with the thread class because the static method has only one instance in memory and does not create an instance with new objects.

Method Three: code block synchronization Lock

classDemo_threadImplementsrunnable{ Public Static intsum = 0;  Public voidAdd () { for(inti=0;i<5000;i++){            synchronized( This){//Use the Code snippet Sync lock to select a sync locksum = sum + 1;//a piece of code without the need to lock the entire method            }        }    }     Public voidRun () {add (); }}

Code block synchronization Lock is also divided into dynamic and static, the same as the method synchronous lock, the definition of dynamic static code block synchronous Lock method is the difference between synchronized () is the parameter is dynamic or static, note that its received parameter should be an instance object, The This in the above method is the pass itself, which belongs to the dynamic method. Here's how to define a dynamic code block synchronization lock:

classobject{//define an empty class to give synchronized a parameter}classDemo_thread2extendsthread{ Public Static intsum = 0;  Public StaticObject lock =NewObject ();//instantiate a static object     Public voidAdd () { for(inti=0;i<5000;i++){             synchronized(lock) {sum= sum + 1; }        }    }     Public voidRun () {add (); }}

Method Four: Use the Lock method

ImportJava.util.concurrent.locks.ReentrantLock;classDemo_thread3Implementsrunnable{ Public Static intsum = 0; Private FinalReentrantlock lock=NewReentrantlock ();//Creating a Reentrantlock K object     Public voidAdd () {lock.lock ();//Gets the lock if the lock is persisted by another thread, for the purpose of thread scheduling,         for(inti=0;i<5000;i++) {//disables the current thread, and the thread will remain dormant until the lock is acquiredsum = sum + 1;    } lock.unlock (); }     Public voidRun () {add (); }}

Because a new lock object is created each time the thread is called, it is necessary to use lock in the thread class to change the Reentrantlock modifier to static.

Java Synchronous Lock method

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.