• August 29, 2014
  • Core Java
  • Comments Off on Synchronized method in java

Synchronized method in java

Synchronized method:

A method declared with synchronized keyword is known as synchronized method. A synchronized method can be static or non-static.

Example: multithreading example without synchronization.

MultiThreadExample.java

/**
 * This program is used to show the multithreading 
 * example without synchronization.
 * @author javawithease
 */
class PrintTable{    
	//not-synchronized method.
	public void printTable(int n){  
       System.out.println("Table of " + n);
       for(int i=1;i<=10;i++){
           System.out.println(n*i);  
           try{  
        	 Thread.sleep(500);  
           }catch(Exception e){
        	 System.out.println(e);
           }  
        }       
     }   
}  
 
class MyThread1 extends Thread{  
    PrintTable pt;  
    MyThread1(PrintTable pt){  
    	this.pt=pt;  
    }  
    public void run(){ 
    	pt.printTable(2);  
    }        
}  
 
class MyThread2 extends Thread{  
	PrintTable pt;  
	MyThread2(PrintTable pt){  
		this.pt=pt;  
	}  
	public void run(){  
		pt.printTable(5);  
	}  
}  
 
public class MultiThreadExample{  
    public static void main(String args[]){
    	//creating PrintTable object.
    	PrintTable obj = new PrintTable();  
 
    	//creating threads.
	    MyThread1 t1=new MyThread1(obj);  
	    MyThread2 t2=new MyThread2(obj);  
 
	    //start threads.
	    t1.start();  
	    t2.start();  
    }  
}

Output:

Table of 5
Table of 2
2
5
4
10
6
15
8
20
10
25
12
30
14
35
16
40
18
45
50
20

Download this example.

Example: Non-static synchronized method.

MultiThreadExample.java

/**
 * This program is used to show the multithreading 
 * example with synchronization.
 * @author javawithease
 */
class PrintTable{    
	//synchronized method.
    public synchronized void printTable(int n){  
       System.out.println("Table of " + n);
       for(int i=1;i<=10;i++){
           System.out.println(n*i);  
           try{  
        	 Thread.sleep(500);  
           }catch(Exception e){
        	 System.out.println(e);
           }  
        }       
    }  
}  
 
class MyThread1 extends Thread{  
    PrintTable pt;  
    MyThread1(PrintTable pt){  
    	this.pt=pt;  
    }  
    public void run(){ 
    	pt.printTable(2);  
    }        
}  
 
class MyThread2 extends Thread{  
	PrintTable pt;  
	MyThread2(PrintTable pt){  
		this.pt=pt;  
	}  
	public void run(){  
		pt.printTable(5);  
	}  
}  
 
public class MultiThreadExample{  
    public static void main(String args[]){
    	//creating PrintTable object.
    	PrintTable obj = new PrintTable();  
 
    	//creating threads.
	    MyThread1 t1=new MyThread1(obj);  
	    MyThread2 t2=new MyThread2(obj);  
 
	    //start threads.
	    t1.start();  
	    t2.start();  
    }  
}

Output:

Table of 2
2
4
6
8
10
12
14
16
18
20
Table of 5
5
10
15
20
25
30
35
40
45
50

Download this example.
 
Next Topic: Static synchronization in java with example.
Previous Topic: Synchronization in java with example.

Related Topics:

Multithreading in java.
Thread life cycle in java.
Way of creating thread in java.
Commonly used methods of Thread class.
Thread scheduling in java with example.
Thread priority in java with example.
Naming a thread in java with example.
Joining a thread in java with example.
Daemon thread in java with example.
Can we start a thread twice?
Can we call run method directly?
Difference between Thread.yield() and Thread.sleep() methods.
Deadlock in java with example.
Starvation in java with example.
Inter-thread communication in java with example.
Synchronization in java with example.
Synchronized method in java with example.
Static synchronization in java with example.
Synchronized block in java with example.

 


Comments are closed.