Java class lock, object lock, Private lock conflict test _java

Source: Internet
Author: User
Tags sleep thread class

Are class locks and object locks conflicting? are object locks and private locks conflicting? Explained by an instance.

I. Relevant agreement

To clarify the description of the following article, first of all the relevant definitions of the lock involved in this article are as follows:

1. Class Lock: A static and synchronized lock is added to the method in the code, or the code snippet of the synchronized (Xxx.class) is increament () in the following text;

2. Object Lock: A synchronized lock is added to the method in the code, or the code snippet for synchronized (this) is Synonmethod () and Syninmethod () as follows;

3. Private Lock: Declare a private property inside the class such as private Object lock, synchronized (lock) in the code segment that needs to be locked, and synmethodwithobj () in the following text.

Second, test code

1. Write a startup class Objectlock

Copy Code code as follows:

public class Objectlock {
public static void Main (string[] args) {
System.out.println ("Start time =" + system.currenttimemillis () + "MS");
Locktestclass test = new Locktestclass ();
for (int i = 0; i < 3; i++) {
Thread thread = new Objthread (test, I);
Thread.Start ();
}
}
}

2. Write a thread class Objthread to start the synchronization method (note that its run method may be tuned for different tests)

Copy Code code as follows:

public class Objthread extends Thread {
Locktestclass lock;
int i = 0;

Public Objthread (Locktestclass lock, int i) {
This.lock = lock;
THIS.I = i;
}

public void Run () {
No Lock method
Lock.nosynmethod (This.getid (), this);
Object Lock Method 1, using synchronized syninmethod approach
Lock.syninmethod ();
Object Lock Method 2, using the synchronized (this) approach
Lock.synonmethod ();
Private lock method, using synchronized (object)
Lock.synmethodwithobj ();
Class locking method, using static synchronized increment mode
Locktestclass.increment ();
}
}

3. Write a test class Locktestclass for the lock, including various locking methods

Copy Code code as follows:

public class Locktestclass {
For class lock Count
private static int i = 0;
Private lock
Private object = new Object ();

/**
* <p>
* No Lock method
*
* @param ThreadID
* @param thread
*/
public void Nosynmethod (long ThreadID, Objthread thread) {
System.out.println ("Nosyn:class obj is" + Thread + ", ThreadId is"
+ ThreadID);
}

 /**
  * Object Lock Method 1
  */
 public synchronized void Synonmethod () {
  system. Out.println ("Synonmethod begins" + ", time ="
    + system.currenttimemillis () + "MS");
  try {
   thread.sleep (2000L);
  } catch (Interruptedexception e) {
& Nbsp;  e.printstacktrace ();
  }
  system.out.println ("Synonmethod ends");
 }

/**
* Object Lock Method 2, using synchronized (this) to add locks
*/
public void Syninmethod () {
Synchronized (this) {
System.out.println ("Syninmethod begins" + ", time ="
+ system.currenttimemillis () + "MS");
try {
Thread.Sleep (2000L);
catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("Syninmethod ends");
}

}

 /**
  * Object Lock Method 3
  */
 public void Synmethodwithobj () {
  synchronized (o bject) {
   system.out.println ("synmethodwithobj begins" + ", time ="
    & nbsp;+ System.currenttimemillis () + "MS");
   try {
    thread.sleep (2000L);
   } catch ( Interruptedexception e) {
    e.printstacktrace ();
   }
    system.out.println ("synmethodwithobj ends");
  }
 }

/**
* Class Lock
*/
public static synchronized void Increament () {
SYSTEM.OUT.PRINTLN ("Class synchronized.") i = "+ i +", time = "
+ system.currenttimemillis () + "MS");
i++;
try {
Thread.Sleep (2000L);
catch (Interruptedexception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("Class synchronized ends.");
}

}

Third, test results

1. Test class locks and object locks, the Objectthread Run method is modified as follows:

Copy Code code as follows:

public void Run () {
No Lock method
Lock.nosynmethod (This.getid (), this);
Object Lock Method 1, using synchronized syninmethod approach
Lock.syninmethod ();
Object Lock Method 2, using the synchronized (this) approach
Lock.synonmethod ();
Private lock method, using synchronized (object)
Lock.synmethodwithobj ();
Class locking method, using static synchronized increment mode
Locktestclass.increament ();
}

Terminal output:

Copy Code code as follows:

Start time = 1413101360231ms
Syninmethod begins, time = 1413101360233ms
Syninmethod ends
Class synchronized. i = 0, time = 1413101362233ms
Syninmethod begins, time = 1413101362233ms
Class synchronized ends.
Syninmethod ends
Class synchronized. i = 1, time = 1413101364233ms
Syninmethod begins, time = 1413101364233ms
Class synchronized ends.
Syninmethod ends
Class synchronized. i = 2, time = 1413101366234ms
Class synchronized ends.

You can see that the object Lock method (Syninmothod) is 2 seconds faster than the class lock method (Increament) when it first starts, because the increament that sleep for 2 seconds while the syninmehtod is executing, and the two methods share one thread, can slow 2 seconds , if the increament in run in front of the Syninmethod, then the first time to start is increament fast 2 seconds.

When the class lock method starts, the object lock method of the other thread also starts almost simultaneously, indicating that the two are not using the same lock and will not produce competition.

Conclusion: Class lock and Object lock will not produce competition, and the locking method will not affect each other.

2. Private locks and Object locks, the Objectthread Run method is modified as follows:

Copy Code code as follows:

public void Run () {
No Lock method
Lock.nosynmethod (This.getid (), this);
Object Lock Method 1, using synchronized syninmethod approach
Lock.syninmethod ();
Object Lock Method 2, using the synchronized (this) approach
Lock.synonmethod ();
Private lock method, using synchronized (object)
Lock.synmethodwithobj ();
Class locking method, using static synchronized increment mode
Locktestclass.increament ();
}

Terminal output:

Copy Code code as follows:

Start time = 1413121912406ms
Syninmethod begins, time = 1413121912407ms.
Syninmethod ends.
Synmethodwithobj begins, time = 1413121914407ms
Syninmethod begins, time = 1413121914407ms.
Syninmethod ends.
Synmethodwithobj ends
Syninmethod begins, time = 1413121916407ms.
Synmethodwithobj begins, time = 1413121916407ms
Syninmethod ends.
Synmethodwithobj ends
Synmethodwithobj begins, time = 1413121918407ms
Synmethodwithobj ends

and class locks and object locks are very similar.

Conclusion: Private lock and Object lock will not compete, and the locking method will not affect each other.

3.synchronized is added directly to the method and synchronized (this), the Objectthread Run method is modified as follows:

Copy Code code as follows:

public void Run () {
No Lock method
Lock.nosynmethod (This.getid (), this);
Object Lock Method 1, using synchronized syninmethod approach
Lock.syninmethod ();
Object Lock Method 2, using the synchronized (this) approach
Lock.synonmethod ();
Private lock method, using synchronized (object)
Lock.synmethodwithobj ();
Class locking method, using static synchronized increment mode
Locktestclass.increament ();
}

Terminal output:

Copy Code code as follows:

Start time = 1413102913278ms
Syninmethod begins, time = 1413102913279ms
Syninmethod ends
Syninmethod begins, time = 1413102915279ms
Syninmethod ends
Synonmethod begins, time = 1413102917279ms
Synonmethod ends
Syninmethod begins, time = 1413102919279ms
Syninmethod ends
Synonmethod begins, time = 1413102921279ms
Synonmethod ends
Synonmethod begins, time = 1413102923279ms
Synonmethod ends

As you can see, the two are strictly serial outputs (of course running Syninmethod first or running Synonmethod is not certain, depending on who gets the lock).

Conclusion: Synchronized directly on the method and synchronized (this) is to lock the current object, the locking method is enough to become a competitive relationship, only one method can be executed at the same time.

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.