Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why synchronize is required when call the wait/nofity method?  RSS feed

 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any insight? thanks.
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I want insight into a method from the standard Java API, my first stop is always the API documentation. And if you look at the docs for Object, then sure enough the docs for those methods say

This method should only be called by a thread that is the owner of this object's monitor.
 
luri ron
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so why this condition is required?
 
Paul Clapham
Sheriff
Posts: 22503
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short answer: because the specs say so.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Longer answer: the whole point of wait/notify is so that one thread can wait for another to do something. And "something" could be almost anything, but it usually involves the exchange of some mutable data. At a minimum, it probably involves one thread asking another, "are you done yet with that thing I asked you to do?" Where "are you done yet?" is an example of mutable state, as over time it changes from "no" to "yes". And often it involves the exchange of more data, such as the result whatever operation one thread was waiting for the other to do. So, if two threads are exchanging mutable data, they need to be using synchronization.

Well, that was the original idea. Nowadays they could be using several other techniques instead. But in Java's original design, synchronization was intended to be the primary and perhaps only way for threads to exchange mutable data.

Anyway, the idea was that if you're doing wait/notify to control when one thread passes data to another, you almost always need(-ed) to use synchronization of some sort to ensure the data was passed safely. Forgetting this might be a common programming error, hard to detect or debug. So Java's creators decided to automatically check for this, by requiring that any use of wait() or notify() be done only if you already own the lock on whatever object you're using to invoke those methods.

One might imagine, with hindsight, other methods, possibly better methods, for achieving this goal. Still, it achieved the basic objective: people using wait/notify without using synchronization are probably doing something wrong. Throwing an error makes this more immediately obvious, and encourages people to learn a little more about threads before they try again.

That's how I see it, anyway. I don't work for Sun, and I'm just guessing as to their motivations. But it makes sense to me.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!