Question: Write a program to implement synchronization.
class Callme
{
public
synchronized void call(String msg)
{
System.out.print("["+msg);
try
{
Thread.sleep(1000);
}
catch(InterruptedException
e)
{
System.out.println(e);
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String
s;
Callme
target;
Thread
t;
Caller(Callmetar,Stringmsg)
{
t
= new Thread(this);
target
= tar;
s
= msg;
t.start();
}
public
void run()
{
target.call(s);
}
}
class Sync
{
public
static void main(String args[])
{
Callmeob
= new Callme();
Caller
o1 = new Caller(ob,"Hello");
Caller
o2 = new Caller(ob,"Synchronized");
Caller
o3 = new Caller(ob,"World");
try
{
o1.t.join();
o2.t.join();
o3.t.join();
}
catch(InterruptedException
e)
{
System.out.println(e);
}
}
}
OUTPUT:
Keyword:
How to recover the classnotfoundexception in Java?
What are the top most requirements or expectations of client for develop Java web application?
Java program question paper in 2020
java applet
creating applet in java
Comments
Post a Comment