Synchronization in Java--sample program __java

Source: Internet
Author: User
Reprint please indicate the source: http://blog.csdn.net/zhangxingping

The synchronization problem in Java is mentioned earlier. The following is a small instance program to demonstrate the synchronization method in Java. A slight modification was made to the counter class mentioned earlier.

public class Counter {
	private int c = 0;

	public void Increment () {
		System.out.println ("before increment, C =" + C);
		C + +;
		try {
			thread.sleep (5);
		} catch (Interruptedexception e) {
			e.printstacktrace ();
		}
		System.out.println ("after increment, C =" + C);
	}

	Public  void Decrement () {
		System.out.println ("before decrement, C =" + C);
		c--;
		try {
			thread.sleep (5);
		} catch (Interruptedexception e) {
			e.printstacktrace ();
		}
		System.out.println ("after decrement, C =" + C);
	}

	public int value () {return
		C;
	}

}

In the implementation of the counter class above, the call to Sleep (5) was added to the increment and decrement methods respectively, in order to magnify the interleaving effect of two threads on the same object's method invocation.


Here are two threads. 10 increment () methods were invoked in Threada, and 10 decrement () methods were called in THREADB.

Thread:

public class Threada implements Runnable {
	private Counter C;

	Public Threada (Counter c) {
		this.c = c;

	}

	@Override public
	Void Run () {(

		int i = 0; i < i++) {
			this.c.increment ();

}}}

THREADB:

public class Threadb implements Runnable {


	private Counter C;


	Public threadb (Counter c) {
		this.c = c;
	}


	@Override public
	Void Run () {(
		int i = 0; i < i++) {
			this.c.decrement ();


}}}


The main program is as follows: it generates two threads Threada and threadb, they share counter C.

public class Main {public

	static void Main (string[] args) {
		Counter c = new Counter ();
		Threada a = new Threada (c);
		THREADB B = new threadb (c);
		
		Thread Threada = new Thread (a);
		Thread threadb = new Thread (b);
		Threada.start ();
		Threadb.start ();
	}


To execute the above code, the possible results are as follows:

Before increment, c = 0
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After increment, C = 1
Before increment, C = 1
After decrement, C = 2
Before decrement, C = 2
After decrement, C = 1
Before decrement, C = 1
After increment, c = 0
Before increment, c = 0
After increment, C = 1
Before increment, C = 1
After decrement, C = 2
Before decrement, C = 2
After increment, C = 1
After decrement, C = 1
Before decrement, C = 1
After decrement, C = 0

It is not difficult to see from the output above that there is a serious staggered phenomenon. The output of before and after in increment or decrement methods should be sequential in pairs. But the output is not so.


After the increment () and decrement () methods of the above code are decorated with synchronized, rerun the program, and the output is as follows:

Before increment, c = 0
After increment, C = 1
Before increment, C = 1
After increment, C = 2
Before decrement, C = 2
After decrement, C = 1
Before increment, C = 1
After increment, C = 2
Before decrement, C = 2
After decrement, C = 1
Before decrement, C = 1
After decrement, C = 0
Before decrement, C = 0
After decrement, C =-1
Before decrement, C =-1
After decrement, C =-2
Before increment, C =-2
After increment, C =-1
Before decrement, C =-1
After decrement, C =-2
Before decrement, C =-2
After decrement, C =-3
Before decrement, C =-3
After decrement, C =-4
Before decrement, C =-4
After decrement, C =-5
Before increment, C =-5
After increment, C =-4
Before decrement, C =-4
After decrement, C =-5
Before increment, C =-5
After increment, C =-4
Before increment, C =-4
After increment, C =-3
Before increment, C =-3
After increment, C =-2
Before increment, C =-2
After increment, C =-1
Before increment, C =-1
After increment, c = 0

This output is not the same as when the synchronized modifier is not added: the single increment and decrement methods do not appear interlaced. Only 10 consecutive increment () and decrement () have staggered.


At the very least, we can see the effect of the Synchronized method from the above example program.


In addition, we can use synchronous statements (that is, synchronous locks using counter objects) to replace the above synchronization methods, the effect is the same. Interested netizens can try it on their own.


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.