• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conditions in Synchronized Blocks  RSS feed

 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard it said that one should never use an if condition in a synchronized block (and should replace it with a while) because if a thread slices out in the middle of executing the if condition then the condition that it was attempting to evaluate before being sliced out could be different when that thread slices back in because another thread could have changed it and this a problem because an if (unlike a while) continues execution from where it left off.

I have done this with my code and have found that it produces peculiar looking code. For example:



In this code will there be an endless stream of exceptions thrown? Are there cases when we can't just simply change 'if' to 'while'?

Cheers
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem that you have mentioned might occur after using a while aslo.
How will a while instead of if will make any difference.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, i'm lost. a synchronized block is used for not slicing away, eh?

or, the other way round - if a thread slices away for some VM-reasons i might not know, way should it slice away differently outside of a synchronized?

can someone please clarify?

thanks,
jan
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Jones:


In this code will there be an endless stream of exceptions thrown? Are there cases when we can't just simply change 'if' to 'while'?


This code will either throw an exception once (which as written will terminate both the while loop and the synchronized block), or not at all execute the loop. So it's totally equivalent to an if - a while doesn't make a different here at all.

You might be confusing this general case with a more specific idiom: always executing a wait() inside a while loop. The reason for that is that you typically can't tell who notified the thread, so you will have to check whether the condition you waited for actually occured - and continue to wait when not.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ilja]: You might be confusing this general case with a more specific idiom: always executing a wait() inside a while loop.

I agree. If you're using wait(), then it makes sense to never use if to check the condition, always a loop instead. But if you're not using wait(), then that advice really doesn't make much sense. I think either this advice was misremembered, or the person who gave it was confused.
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i have an stupid question.



What happend to the thread that is waiting for the object lock, when the thread is awakened? It continues before wait() or goes inside to test the loop again?

thanks very much !!!
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i supose in the code i have written that wait is called in a synchronized method !!!
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im sorry for the english, since its not my language

What happens to the thread that is waiting for the object�s lock, when the thread is awakened? it continues AFTER wait() or goes to test the loop again?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens to the thread that is waiting for the object�s lock, when the thread is awakened?


The thread is waiting (for notification) by calling the wait() method. When it gets the notification (ie. awaken), then it must reacquire the lock (ie. wait for the lock).

it continues AFTER wait() or goes to test the loop again?


Or both...

It continues AFTER wait(), which in your example, in still within the loop.

Henry
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry !!!

so when the thread is awakened will the comment code execute?
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im asking this, because i was reading this link http://java.sun.com/docs/books/tutorial/essential/concurrency/guardmeth.html

and was wondering if i could put the

empty = true or empty = false inside the while loop, after wait.
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cristiano Sganzerla:

so when the thread is awakened will the comment code execute?


Well, I guess it will depend on how the wait() method is awaken. If the wait() method just returns, because of a notify() call, then it will run the next line of code. If the wait() method throws an interrupt exception, because of an interrupt() call, then it will go to the catch clause.

This is no different from how any other method call returns.

Henry
[ January 21, 2007: Message edited by: Henry Wong ]
 
Cristiano Sganzerla
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again !!!
 
Harjit Singh
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While loop is required and is the best way to handle this situation because in case you have an if the control will come to the next statement in an if block and the condition may never be executed. In case of a while the condition is tested and then the while block is entered thats the difference.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!