Hi, I am trying to execute the following code to
test the synchronization.
If this is synchronized the outputp will be -
But the system is working fine!! "Thread details"
Is everything fine? first
I knew first
But the system is working fine!! "Thread details"
Is everything fine? second
I knew second
But the system is working fine!! "Thread details"
Is everything fine? third
I knew third
whereas the output is coming as
But the system is working fine!! "Thread details"
Is everything fine? first
But the system is working fine!! "Thread details"
Is everything fine? second
But the system is working fine!! "Thread details"
Is everything fine? third
I knew first
I knew second
I knew third
Please help me in this issue.
Thanks in advance.
bjava.
code -
class prrint
{
public void pg(
String s)
{
try
{
System.out.println("Is everything fine?" + s);
Thread.sleep(1000);
System.out.println("I knew " + s);
}
catch(InterruptedException e)
{
// do no....
}
}
}
class makeTh implements Runnable
{
Thread t;
String s;
makeTh(String ls_n)
{
//t = new Thread(this,ls_n);
s = ls_n;
//t.start();
}
public void run()
{
prrint p = new prrint();
synchronized(p)
{
boolean has_lock;
has_lock = Thread.holdsLock(p);
if (has_lock = true)
{
System.out.println("But the system is working fine!! "+ Thread.currentThread());
}
p.pg(s);
}
}
}
public class syncH
{
public synchronized static void main(String q[])
{
makeTh mt1 = new makeTh("first");
makeTh mt2 = new makeTh("second");
makeTh mt3 = new makeTh("third");
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt2);
Thread t3 = new Thread(mt3);
//mt1.tHH("first");
//mt2.tHH("second");
//mt3.tHH("third");
t1.start();
t2.start();
t3.start();
/*try
{
mt1.join();
mt2.join();
mt3.join();
}
catch(InterruptedException e)
{
// do no....
}*/
}
}