CodesJava

Easy learning with example program codes

Static synchronization in java


Static synchronization is achieved by static synchronized methods. Static synchronized method locked on class and non-static synchronized method locked on current object i.e. static and non-static synchronized methods can run at same time. It can produce inconsistency problem.

If static synchronized method is called a class level lock is acquired and then if an object is tries to access non-static synchronized method at the same time it will not be accessible because class level lock is already acquired.

Example:

MultiThreadExample.java

/**
 * This program is used to show the multithreading 
 * example with synchronization using static synchronized method.
 * @author codesjava
 */
class PrintTable{    
    public synchronized static 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{    
    public void run(){ 
    	PrintTable.printTable(2);  
    }        
}  
 
class MyThread2 extends Thread{   
	public void run(){  
		PrintTable.printTable(5);  
	}  
}  
 
public class MultiThreadExample{  
    public static void main(String args[]){
 
    	//creating threads.
	    MyThread1 t1=new MyThread1();  
	    MyThread2 t2=new MyThread2();  
 
	    //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.

Difference between static and non-static synchronized method.

    Non-Static synchronized method       Static synchronized method
  1. Lock is on object.
  2. Commonly used.
  3. Static keyword is not used.
  1. Lock is on class.
  2. Not commonly used.
  3. Used with static keyword.

 
Next Topic: Synchronized block in java with example.
Previous Topic: Synchronized method in java with example.

Related Topics:

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap