[concurrency-interest] question on concurrent HashMap

Joe Bowbeer joe.bowbeer at gmail.com
Thu Nov 2 20:09:42 EST 2006


On 11/2/06, Amarnath Nanduri <amar_nan at yahoo.com> wrote:
> [...]
> One question that bothers me is that how can I make the
> Timer Task (in manager) and Swing Timer (controlling the gui
> components) be in step. i.e only after the concurrent hashmap
> is populated by the manager, do I want the swing timer firing up
> to allow the gui components to get their data.
>

The timer task in manager can schedule a task on Swing's event thread
using EventQueue.invokeLater (aka SwingUtilities.invokeLater).  This
is the mechanism employed by SwingWorker.  In your case, the task
would query the cache for the latest results and update the UI.

If the contents of the cache are interrelated then you may need to
employ a split-model technique so that your UI depicts a valid
instance of the cached state - and not some transitional state.

More:

SwingWorker is reconstructed in JCiP.

The second example in my Swing Threads article uses the split-model technique:

http://java.sun.com/products/jfc/tsc/articles/threads/threads3.html


> Is there a way for the gui components to wait till their data object
> is put in the concurrent hash map, before they attempt to retrieve
> the data from the concurrent hashmap?
>

Not on the Swing GUI side - without blocking the event thread.
Polling in the event thread (via Swing Timer) would be the only way.

--Joe


On 11/2/06, Amarnath Nanduri <amar_nan at yahoo.com> wrote:
> I am currently using a concurrent hashmap to act as shared memory between
> different threads. I think I might be doing something wrong and would
> appreciate your help and guidance on this.
>
> The scenario is:
> A manager class fires up a bunch of database objects (implementing the
> Callable interface)  every one minute (in a Timer task) and gets back the
> immutable data objects.
> //sample code
> ExecutorService.invokeAll(database objects implementing Callable interface).
>
> The manager then updates a concurrent HashMap with these data objects.
>
> The gui components request the manager for the concurrent HashMap and each
> gui component asks for its own data object out of the cncurrent hash map.
> All gui components are in a single swing timer that fires up an event once
> every minute and asks the gui components to get their data objects.
>
> Both Timer Task and Swing Timer are fired every one minute. I currently
> guess the amount of time delay I need (set to 30 seconds)  (to get the data
> from the database and populate the concurrent hash map) before the swing
> timer fires an event for the gui components to fetch from the concurrent
> hash map.
>
> [...]


More information about the Concurrency-interest mailing list