This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Please help." Watch "Please help." New topic
Author

Please help.

babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
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....
}*/
}
}


Be bold.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

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.


[Jess in Action][AskingGoodQuestions]
babai bhaumik
Greenhorn

Joined: May 12, 2007
Posts: 18
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");
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help.
 
Similar Threads
Thread Behaviour? Help!!!
synchronized and Thread
notifyAll()
urgent answer required!!
help regarding synchronized methods