JAVA synchronized keyword lock mechanism (middle)

Source: Internet
Author: User

Synchronized lock mechanism simple usage, efficient execution efficiency makes it the first choice to solve thread safety. The following summarizes its characteristics and usage techniques to deepen its understanding.

Characteristics:

1. The Java language keyword, when used to decorate a method or a block of code, guarantees that at most one thread at a time executes the code.

2. When a thread accesses a synchronized (this) synchronization block of object at the same time, other threads can still access a non-decorated method or block of code.

3. When multiple threads access the synchronized (this) synchronous code block of object at the same time, there is mutually exclusive access, and other threads block until the lock is acquired.

4. When a thread accesses the synchronized (this) of object to synchronize a block of code, the same thread can acquire the lock more than once, and of course it does not need to be freed multiple times. Get and release must be the same.

5. All objects can acquire the lock or release the lock.

6. All classes can also acquire locks and release locks, so static methods can also be locked. and the characteristic ibid.

Guess:

each object in the JVM has a status of record locks, which accumulate when the same thread accesses the lock, and other threads access to wait until the state becomes unlocked when the same thread releases the lock.

Question:

so for a class lock, should all objects be able to acquire the lock, then the lock is global and valid for all objects?

 Public classSynchronizedmtdtest { Public Static voidMain (string[] args) {NewThread (NewRunnable () {@Override Public voidrun () {synchronizedmtdtest.appendstr ();    }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {synchronizedmtdtest.printstr ();    }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {synchronizedmtdtest synchronizedmtdtest=Newsynchronizedmtdtest ();      Synchronizedmtdtest.appendstr ();    }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {synchronizedmtdtest synchronizedmtdtest=Newsynchronizedmtdtest ();      Synchronizedmtdtest.printstr ();    }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {synchronizedmtdtest synchronizedmtdtest=Newsynchronizedmtdtest ();      Synchronizedmtdtest.append ();  }}). Start (); }   Public synchronized Static voidAppendstr () {System.out.println ("Pid=" + thread.currentthread (). GetId () + "------appendstr------"); Try{Thread.Sleep (1000); System.out.println ("Pid=" + thread.currentthread (). GetId () + "------appendStr1000------"); } Catch(interruptedexception e) {e.printstacktrace (); }  }   Public Static voidappend () {System.out.println ("Pid=" + thread.currentthread (). GetId () + "------Append------"); Try{Thread.Sleep (3000); System.out.println ("Pid=" + thread.currentthread (). GetId () + "------append3000------"); } Catch(interruptedexception e) {e.printstacktrace (); }  }   Public synchronized Static voidPrintstr () {System.out.println ("Pid=" + thread.currentthread (). GetId () + "------printstr------"); Try{Thread.Sleep (6000); System.out.println ("Pid=" + thread.currentthread (). GetId () + "------printStr6000------"); } Catch(interruptedexception e) {e.printstacktrace (); }  }}

Results:

pid=10------APPENDSTR------pid=14------APPEND------pid=10------appendStr1000------pid=13------ PRINTSTR------pid=14------append3000------pid=13------printStr6000------pid=12------APPENDSTR------pid=12----- -appendstr1000------pid=11------PRINTSTR------pid=11------printStr6000------

The result of the analysis shows that for static methods to be locked, all thread invocation methods are mutually exclusive, and the lock is not mutually exclusive.

So:

for class locks, it should be in the persistence generation, that is, the method area has a locking mechanism for the specific class, and the principle of the same object lock.

So:

Visible, the above can do the following changes to achieve the same effect.

public static void Appendstr () {    synchronized (synchronizedmtdtest.class) {      System.out.println ("pid=" + Thread.CurrentThread (). GetId () + "------appendstr------");      try {        thread.sleep (+);        System.out.println ("pid=" + thread.currentthread (). GetId () + "------appendStr1000------");      } catch (Interruptedexception e) {        e.printstacktrace ();}}  }  public static void Printstr () {    synchronized (synchronizedmtdtest.class) {      System.out.println ("pid=" + Thread.CurrentThread (). GetId () + "------printstr------");      try {        Thread.Sleep (6000);        System.out.println ("pid=" + thread.currentthread (). GetId () + "------printStr6000------");      } catch (Interruptedexception e) {        e.printstacktrace ();}}  }

Results:

pid=10------appendstr------pid=10------appendStr1000------pid=13------printstr------PID =13------printStr6000------pid=12------appendstr------pid=12------appendStr1000------ pid=11------printstr------pid=11------printStr6000------

JAVA synchronized keyword lock mechanism (middle)

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.