• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I can't synchronize my threads properly

 
Zac Roberts
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tarik Makota
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
satya rao
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic