Using Synchronization Method In Java

CODE_

package threading.in.java;
public class Synchronization extends Thread
{
private int interval,threadno;

public static void main(String[] args) {
for(int i=1;i<=2;i++)
{
Synchronization s1=new Synchronization(1000, i);
s1.start();
}
}

public Synchronization(int interval,int threadno) {
this.interval = interval;
this.threadno=threadno;
}

public static synchronized void Counter(int threadno,int interval){
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(threadno + ” = ” + i);
Thread.sleep(interval);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}

}
public void run(){
{
Counter(threadno, interval);
}

}

}

 

 

OUTPUT OF THE FOLLOWING PROGRAM_

Leave a Reply