Synchronized keywords in Java

Source: Internet
Author: User

Every object in Java has an object lock, and synchronized is going to get this lock, see the example below

ImportJava.util.Random; Public classmydata{ Public synchronized voidincrement () { for(inti = 0; I < 10; i++) {            Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }         Public synchronized voidDecrement () { for(inti = 0; I < 10; i++) {            Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }         Public Static voidMain (string[] args) {FinalMyData myData1 =NewMyData (); //final MyData myData2 = new MyData ();       NewThread (NewRunnable () {@Override Public voidrun () {mydata1.increment ();                }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {mydata1.decrement ();    }}). Start (); }}

No matter how many times the execution is ordered, two threads operate on the same object, the first executing thread gets a lock, and the second thread can only wait until the first thread finishes executing to get the lock and enter the method.

Let's look at the following example

ImportJava.util.Random; Public classmydata{ Public synchronized voidincrement () { for(inti = 0; I < 10; i++) {            Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }         Public synchronized voidDecrement () { for(inti = 0; I < 10; i++) {            Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }         Public Static voidMain (string[] args) {FinalMyData myData1 =NewMyData (); FinalMyData MyData2 =NewMyData (); NewThread (NewRunnable () {@Override Public voidrun () {mydata1.increment ();                }}). Start (); NewThread (NewRunnable () {@Override Public voidrun () {mydata2.decrement ();    }}). Start (); }}

The result of execution is unordered, two objects, two locks, so they do not affect each other, each executes their own.

Let's take a look at the following example

Import Java.util.Random;
public class mydata{

public synchronized void increment () {
for (int i = 0; i < i++) {
try {
Thread.Sleep (New Random (). Nextint (200));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}

public static synchronized void decrement () {
for (int i = 0; i < i++) {
try {
Thread.Sleep (New Random (). Nextint (200));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
}
}

public static void Main (string[] args) {
Final MyData myData1 = new MyData ();
Final MyData myData2 = new MyData ();
New Thread (New Runnable () {
@Override
public void Run () {
Mydata1.increment ();
}
}). Start ();

New Thread (New Runnable () {
@Override
public void Run () {
Mydata.decrement ();
}
}). Start ();
}
}

The result is also unordered, the same reason as above, the static method belongs to the class object, so the decrement method locks the Mydata.class object, and mydata1.increment (); The lock is a MyData1 object that does not interfere with each other.

Just remember that the synchronized lock is an object, each object has an object lock, to get the lock to execute synchronized method

Synchronized keywords in Java

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.