Java Synchronized

Memory Consistency Errors are common problems with Multi Thread Programming. In the tutorial, JavaSampleApproach will introduce you about Java Synchronized, is one of the solution for resolving problems related with Consistency Errors.

A. Concepts

When program has a shared resource with many threads, Memory Consistency Error will come when at a specific time, multi threads try to access the resource with 2 operations: read() & write() (called: Thread Interference). For facing with Thread Interference, Java provides a synchronized methodology that can apply on: synchronized method, synchronized statement( synchronized block).

B. Practice

I. Technologies

– Java 8
– Editor: Eclipse

II. Problem Sample

What happens with below programming? It will never terminate?

JavaSynchronized class:

package com.javasampleapproach.javasynchronized;

public class JavaSynchronized extends Thread{
	Data data;
	public JavaSynchronized(Data data){
		this.data = data; 
	}
	
	public void run() {
		data.count();
    }
	
	public static void main(String[] args) {
		Data data = new Data();
		do{
			data.set(0);
			for(int i=0; i<10000; i++){
				 (new JavaSynchronized(data)).start();
			}
			
			try{
				Thread.sleep(1000);
				System.out.println(data.get());
			}catch(Exception e){
			}	
		}while(data.get() == 10000);
	}
}

Data class

package com.javasampleapproach.javasynchronized;

public class Data {
	int a = 0;
	
	public int get(){
		return a;
	}
	
	public void set(int a){
		this.a = a;
	}
	
	public void count(){
		a++;
	}
}

A programming has a shared source: Data data, for increasing value of a member, the program sets up 10000 threads which each thread will increase value of a member by one unit.

Expected value of a member after 10000 thread is 10000. But base on theory, sometimes the result is not expected (not 10000) because of Memory Consistency Error by Thread Interference. So do-while statement will help to reproduce the problems:

Result:
synchronized method problems

III. Solution with Java Synchronized


Java Synchronized
will help programming resolving Memory Consistency Error.

- Solution with Java Synchronized Method
Apply for count() method of Data class.

public synchronized void count(){
	a++;
}

- Solution with Java Synchronized Statement
Apply for body of count() method in Data class.

public void count(){
	synchronized(this){
		a++;	
	}
}

So now below code will never terminate:

package com.javasampleapproach.javasynchronized;

public class JavaSynchronized extends Thread{
	Data data;
	public JavaSynchronized(Data data){
		this.data = data; 
	}
	
	public void run() {
		data.count();
    }
	
	public static void main(String[] args) {
		Data data = new Data();
		do{
			data.set(0);
			for(int i=0; i<10000; i++){
				 (new JavaSynchronized(data)).start();
			}
			
			try{
				Thread.sleep(1000);
				System.out.println(data.get());
			}catch(Exception e){
			}	
		}while(data.get() == 10000);
	}
}

IV. Sourcecode

Java Synchronized

3 thoughts on “Java Synchronized”

  1. Somebody essentially lend a hand to make severely posts I’d state.
    This is the first time I frequented your web page and so far?
    I amazed with the analysis you made to make this particular submit incredible.
    Great activity!

Leave a Reply

Your email address will not be published. Required fields are marked *