• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification required on synchronized block

 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two list a, b. One thread is adding the data into the list and start the timer if the timer is not already started.



Another thread is a timer Task thread that will wake up for every 5 secs to read the list and process as follows.



Could any clarify my questions in the comments of the code. if my logic is wrong please provide the better way to implement to avoid synchronization issue.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to 1 is yes but I think you have more problems than that.

I'd simplify this down to synchronisation that covered A & B for reads and writes, with a single object to signal between threads and then optimise it later if you have to, you seem to be struggling a bit. If your not 100% confident simple is always best.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must synchronize on both lists before looking at their combined
size. Otherwise, one of the add() methods could loose an object while
MyTask thinks the lists are empty. To process the non-empty lists, you
can synchronize separately again if you wish.
Jim ... ...
 
shan raj
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestions.

Chris Hurst ,

Could you provide sample program or tutorial to use the single object to signal between the thread.


I will also try "Jim Hoglund" suggestion given in the previous posts.

Regards
Shan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic