0% found this document useful (0 votes)
3 views3 pages

Synchronized

The document contains Java code that demonstrates the use of synchronized methods to control access to a shared resource. It defines a 'Callme' class with a synchronized method 'call' that prints messages with a delay, and a 'Caller' class that implements Runnable to create threads. The 'Synch' class initializes multiple 'Caller' objects and waits for their threads to complete execution.

Uploaded by

dakshini742006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Synchronized

The document contains Java code that demonstrates the use of synchronized methods to control access to a shared resource. It defines a 'Callme' class with a synchronized method 'call' that prints messages with a delay, and a 'Caller' class that implements Runnable to create threads. The 'Synch' class initializes multiple 'Caller' objects and waits for their threads to complete execution.

Uploaded by

dakshini742006
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

class Callme

{
synchronized void call(String msg)
{
[Link]("[" + msg);
try
{
[Link](1000);
}
catch(InterruptedException e)
{
[Link]("Interrupted");
}
[Link]("]");
}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s)
{
target = targ;
msg = s;
t = new Thread(this);
[Link]();
}
public void run()
{
[Link](msg);//Synchronized Method
}
}

class Synch {
public static void main(String args[])

{
Callme target = new Callme();
Caller ob1 = new Caller(target,
"Hello");
Caller ob2 = new Caller(target,
"Synchronized");
Caller ob3 = new Caller(target,
"World");
// wait for threads to end
try {
[Link]();
[Link]();
[Link]();
} catch(InterruptedException e)
{
[Link]("Interrupted");
}
}
}

You might also like