Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : Multi-threaded linked list access synchronization

My methodology may be wrong here (or implemented better). If so, please let me know of a better idea.

I am trying to implement a shape file reader that works with local files or over a network. However, some of the shapefiles are HUGE, and could take a while to completely load. I would like to separate the loading class into its own thread. I would also like for the main thread to extract the loaded data from the loader, while it is still loading, so it can be displayed immediately.

To accomplish this, I figured I could maintain a linked list within the loader containing the elements that have already been completely loaded. The main thread can poll the loader to see if any elements have been loaded, and extract the head of the linked list, which would remove it from the loader.

What mechanisms can I use to ensure that the main class and the loader class do not attempt to access the head of the linked list at the same time? I know this involves synchronization, but I am a total noob with Java threads. Just need someone to point me in the right direction.

If you have a different idea altogether, I'm all ears. Thanks

like image 461
William the Coderer Avatar asked Mar 19 '26 01:03

William the Coderer


1 Answers

doesn't this sound like a producer-consumer problem? Easiest way will be to use BlockingQueue

eg: Use the concept shown here.. http://www.java2s.com/Code/Java/JDK-6/ProducerandconsumerbasedonBlockingQueue.htm

like image 108
Anish Dasappan Avatar answered Mar 20 '26 14:03

Anish Dasappan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!