Forums Register Login

Clarification required on synchronized block

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ... ...
+Pie Number of slices to send: Send
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
Seriously Rick? Seriously? You might as well just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1145 times.
Similar Threads
Problem in arraylist
Multiple thread accessing List (ArrayList, Vector)
ConcurrentModificationException when running this program
null check in Iterator
Producer-Consumer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:39:58.