Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Please help.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
}*/
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Well, there are lots of things going on here, but the one that most strongly affects the outcome of your program is that each thread creates its own prrint object. Therefore, the three threads are synchronizing on separate locks -- they're completely independent. To get the behavior you expect, the three threads would need to share a single prrint object.

Other issues: inside synchronized(p), Thread.hasLock(p) will always return true, so there's no point; also, your boolean "condition" is actually an assignment. In this case, that doesn't matter, but in other code it certainly would. Make sure you use "==", not "=" to compare values! Actually, don't ever compare to a boolean literal -- instead of writing "if (x == true)", just write "if (x)", and you'll never make this mistake.
 
babai bhaumik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for the reply. Changed(rather I should say washed the code and the results are as expected now.)

following is the washed(I mean changed)... code

class prrint
{
public void pg(String s)
{
try
{
System.out.println("How is life?" + s);
Thread.sleep(1000);
System.out.println("I knew it!" + s);
}
catch(InterruptedException e)
{
// do no....
}
}
}

class makeTh implements Runnable
{
Thread t;
String s;
prrint p;
makeTh(prrint pp,String ls_n)
{
p = pp;
s = ls_n;
Thread t = new Thread(this,ls_n);
t.start();
}

public void run()
{
synchronized(p)
{
p.pg(s);
}
}
}

public class syncH
{
public static void main(String q[])
{
prrint i = new prrint();
makeTh mt1 = new makeTh(i,"First person says");
makeTh mt2 = new makeTh(i,"Second person says");
makeTh mt3 = new makeTh(i,"Third person says");
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic