Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Practical -29

Aim :- Write a program in Java to demonstrate use of synchronization


of threads when multiple threads are trying to update common
variable.

class Counter
{
int c=0;

synchronized void increment()


{
c++;
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("caught: " + e);
}

System.out.println("C = " + c);


}
}

class NewThread extends Thread


{
Counter obj;
NewThread(Counter o)
{
obj = o;
}
public void run()
{
obj.increment();
}
}

public class Example


{
public static void main(String[] args)
{
Counter c1 = new Counter();

NewThread t1 = new NewThread(c1);


t1.start();

NewThread t2 = new NewThread(c1);


t2.start();

NewThread t3 = new NewThread(c1);


t3.start();
}
}

Output:

C=1
C=2
C=3

You might also like