Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the way to use wait and notify?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this program reader thread will wait until writer thread writes data. Once writer writes data then writer will wait till reader thread reads that data and sets isDataAvailable as false. Then writer finds flag as false then again write data vice versa. I wrote below program. Is this the way we should use wait and notify? If yes then if possible how can I improve it or If not please anyone help me to figure it out.

Data Source

Writer CodeReader CodeReaderWriter Demo Yes it runs successfully and also gives output as expected.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:Is this the way we should use wait and notify? If yes then if possible how can I improve it or If not please anyone help me to figure it out.


Arguably, the main purpose of wait & notify is to wait for the *exact* amount of time -- meaning it is waiting for a period of time, exactly when the other thread is ready. It is a little bit more complex than that, but anyway... If you don't wait the exact period of time, then there are two possible issues. Waiting too little will eat up CPU cycles, as the thread is constantly waking up, only to find that the other thread is not ready. Waiting too much will waste time, as there will be periods where the thread is waiting when the other thread is already done.

So, question one. What is the purpose of all those sleep() calls in your code? Are you trying to guess the amount of waiting time? Isn't that what the wait() call is for? The advantage of waiting the exact amount of time is moot here, as your code goes and sleeps some more time.

Question two. Why is the waiting only in one direction? Your reader waits for the writer, which notifies when data is ready. This is fine. But what about the other direction? Shouldn't the writer wait until the reader is done reading too? And the reader can notify the writer that it can write?

Additionally, you have some issues related to lost notifications -- which is technically a bug. However, it is hidden by all those sleep() calls, so you probably won't encounter them, until you try to get rid of the sleep() calls.

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thank you for response
Henry Wong wrote:What is the purpose of all those sleep() calls in your code?
I just used that sleep to see output in slow motion no any other purpose.

Ohh yes writer should also wait until reader reads and notifies else writer thread will keep executing that code am I correct? do you mean I have to write wait code in writer's run method?

lost notifications? you mean if A thread notifies before another thread B signals wait that time waiting thread B missed the notify signal of thread A and these signals are not stored so waiting thread i.e. B keeps waiting for notify signal from A thread. am correct? but in my code I used a boolean flag isDataAvailable for knowing the status of data availability isn't it sufficient ?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:lost notifications? you mean if A thread notifies before another thread B signals wait that time waiting thread B missed the notify signal of thread A and these signals are not stored so waiting thread i.e. B keeps waiting for notify signal from A thread. am correct? but in my code I used a boolean flag isDataAvailable for knowing the status of data availability isn't it sufficient ?


Oops... You are correct. While it is possible to lose notifications, your reader do not wait blindly, and hence, should handle that case.

Ganish Patil wrote:
Ohh yes writer should also wait until reader reads and notifies else writer thread will keep executing that code am I correct? do you mean I have to write wait code in writer's run method?


Well, yeah. How will the writer wait for the reader, unless you have code where the writer waits for the reader?

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed reader and writer code including omitting sleep as it wasn't necessary, please see this
Writer CodeReader Code
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:I changed reader and writer code including omitting sleep as it wasn't necessary


Looks good. Have a cow...

Henry
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The moment of pride to get a cow on successful code Thank you so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!