Reputation: 708
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
Upvotes: 3
Views: 1963
Reputation: 415
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
Upvotes: 2