• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronized and wait+notify... which when?  RSS feed

 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have synchronized way and we have wait+notify way which one to use and when?

Can I say wait+notify would be more efficient when my number of threads increase?

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:We have synchronized way and we have wait+notify way which one to use and when?

Can I say wait+notify would be more efficient when my number of threads increase?


It is not exactly "either/or". In fact, the wait() and notify() mechanism depends on synchronization.

Henry
 
Tom Storm
Ranch Hand
Posts: 31
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronized only means that one thread can have the intrinsic lock of an object at one time. Wait and notify simple suspend a synchronized method and release the intrinsic lock and then notify tells wait to wake up but only releases the lock when it has finished.
Synchronized when accessing a single variable and wait and notify when many threads as there is also notify all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!