Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   2. Concurrency  >   2.3. Method synchronization

2.3. Method synchronization
Last updated: 31 January 2013.

This tutorial will show you how to synchronize a method in Java.

Method synchronization is very simple. You just have to add the keyword synchronized to its signature before the return type. For example, the class Shared shown below is a modifed version of the class that you have seen in the previous tutorial. In this new version, the method incrementArrayElements is now a synchronized method:

public final class Shared {

   
private int[] array = new int[10];

   
public synchronized void incrementArrayElements() throws InterruptedException{
         
for(int i=0; i<array.length; ++i){
               
array[i] = array[i] + 1;
                Thread.sleep
(100);
         
}
    }

   
public int[] getArray(){
         
return array;
   
}

}

The class MyThread from the previous tutorial which uses the class Shared remains unchanged:

public final class MyThread extends Thread {

   
private Shared sharedInstance;

   
public MyThread(Shared sharedInstance){
         
this.sharedInstance = sharedInstance;
   
}

   
public void run(){
         
try{
               
sharedInstance.incrementArrayElements();
         
}
         
catch(InterruptedException e){

          }
    }

}

The class App containing the main method also remains unchanged:

public final class App {

   
public static void main(String[] args){
         
try{
               
Shared sharedInstance = new Shared();

                Thread thread1 =
new MyThread(sharedInstance);
                Thread thread2 =
new MyThread(sharedInstance);

                thread1.start
();
                thread2.start
();

                thread1.join
();
                thread2.join
();

               
for(int i : sharedInstance.getArray())
                     
System.out.println(i);
         
}
         
catch(InterruptedException e){

          }
    }

}

If you run the class App, you will get this output:

2
2
2
2
2
2
2
2
2
2

The execution is a bit slower because the synchronized method incrementArrayElements cannot be executed by both threads simultaneously. When the first thread is executing the method incrementArrayElements, the second thread waits until the first thread has finished, which guarantees exclusive access to the statements in the method incrementArrayElements.

If an object contains several synchronized methods, when one of them is being executed by a thread, the other synchronized methods cannot be executed at the same time by other threads. Of course, if the object also contains methods that are not synchronized, they can be executed concurrently.

The next tutorial will show you how to synchronize a block of code.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   2. Concurrency  >   2.3. Method synchronization
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   2. Concurrency  >   2.4. Block synchronization

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7