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

Program For Synchronization of Multi Threads

This Java program demonstrates synchronization of multiple threads. It defines a Callme class with a call method that prints a message string. A Caller class implements Runnable and calls the call method in a synchronized block. The main method creates Caller objects passing a Callme target and string, starts their threads, and joins them to synchronize output.

Uploaded by

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

Program For Synchronization of Multi Threads

This Java program demonstrates synchronization of multiple threads. It defines a Callme class with a call method that prints a message string. A Caller class implements Runnable and calls the call method in a synchronized block. The main method creates Caller objects passing a Callme target and string, starts their threads, and joins them to synchronize output.

Uploaded by

GokulKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*PROGRAM FOR SYNCHRONIZATION OF MULTI THREADS*/

class Callme
{
void call(String msg)
{
System.out.println("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callme targ,String s)
{
target=targ;
msg=s;
t=new Thread(this);
t.start();
}
public void run()
{
synchronized(target)
{
target.call(msg);
}
}
}
public class Synch
{

public static void main(String args[])


{
System.out.println("After Synchronization");
Callme target=new Callme();
Caller ob1=new Caller(target,"Computer");
Caller ob2=new Caller(target,"Synchronized");
Caller ob3=new Caller(target,"Technology");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}
}
}

C:\gowtham>javac Synch3.java
C:\gowtham>java Synch3
After Synchronization
[Computer]
[Synchronized]
[Technology]

C:\gowtham>javac Synch3.java
C:\gowtham>java Synch3
Before Synchronization
[Computer
[Synchronized
[Technology
]
]
]

You might also like