aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I can't synchronize my threads properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I can Watch "I can New topic
Author

I can't synchronize my threads properly

Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
Can someone tell me where I have gone wrong here:

I thought that this would display 0-9 and then 0-9 again (because of synchronized) but it is giving me more of a jumble of numbers. What am I missing here? Thanks for the help.
Zac


Zac Roberts<br />SCJP2
Tarik Makota
Greenhorn

Joined: Jan 29, 2002
Posts: 23
"When thread finishes sleeping it does not continue executing. It goes into Ready state and needs to contend for CPU/lock"
Tarik
Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
OK this is good, so sleep() releases the lock correct? In JQ+ notes it says "sleep(): Does not release the locks (if any)". So I guess JQ+ is wrong on that one. Thanks
Zac
satya rao
Greenhorn

Joined: Feb 14, 2002
Posts: 9
If you put your try-catch block with Thread.sleep(..) outside for-loop, you can get your desired output i.e., 0-9 and then 0-9.
The following is the status of the threads as the time progresses:
Thread1 Thread2
----------------------------------
ready ......................
non-runnable(sleep)......... ready
............................ non-runnable
output(after elapsed time)..
non-runnable(sleep)......... output
This is the reason why, you are not getting your desired output. What I understood, if a thread is in non-runnable state, then other waiting threads will get a chance to be executed. Also, synchronized method cannot change the status of the object. In this situation, it is just printing a value, but not changing the status of the object.
I am also a beginner. Please correct me if I am wrong.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
ss1225,
Welcome to Javaranch, a friendly place for Java greenhorns
Please, read the Javaranch Naming Policy and change your "Publicly Displayed Name" to comply with the policy. Thank you for your cooperation.
[ February 15, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
satya rao
Greenhorn

Joined: Feb 14, 2002
Posts: 9
Valentin Crettaz,
I changed it.
BTW, I have seen you so many discussion before. Can you throw some light on my response to this question?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
As soon as you change your publicly diplayed name properly as stated in the policy, that is, a first name a space and a last name
Thank you
[ February 15, 2002: Message edited by: Valentin Crettaz ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Zac, JQ+ is correct, sleep does not lose ownership of any monitors. When the thread sleeps it keeps the lock. The problem you have is that p (instance of Another) is an instance member of class Threads. You instantiate two objects of class Threads and thus each of them will end up with their own instance of Another. synchronized methods are only good if two threads try to invoke a method of the same object. But here, you have two threads that invoke a method of two different objects, hence the unpredictable result...
One solution could be to declare p static so that the instance of Another (p) will be the same for all instances of class Threads.
A second solution is to keep p the way it is declared but to make aMethod static so that the class lock is acquired...
The effect of both solutions is the same. It guarantees the expected result, that is, 0-9 0-9
satya rao,
synchronized method cannot change the status of the object.
what do you mean ?
[ February 15, 2002: Message edited by: Valentin Crettaz ]
[ February 15, 2002: Message edited by: Valentin Crettaz ]
satya rao
Greenhorn

Joined: Feb 14, 2002
Posts: 9
I meant, when synchorized method of an object is executed by a thread, another thread can not change the instance values(status) of that object.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sure it can but not through the same synchronized method. If you have another unsynchronized method that deals with the same members than the synchronized, then two threads may change the value of the member variable. This is bad practice though, you should never have a mix of both synchronized and unsynchronized methods that deal with the same members, you may get unexpected results.
Zac Roberts
Ranch Hand

Joined: Jan 29, 2002
Posts: 82
Valentin, to the rescue once again... I am beginning to think you need to change your name to Ol' Faithful (but of course that would be against the rules), anyways, your answer makes perfect sense. Thanks.
Zac
 
jQuery in Action, 2nd edition
 
subject: I can't synchronize my threads properly