[concurrency-interest] SwingWorker a special producer/consumer

Rémi Forax forax at univ-mlv.fr
Fri Jun 2 08:03:28 EDT 2006


Joe Bowbeer a écrit :

>On 6/2/06, Rémi Forax <forax at univ-mlv.fr> wrote:
>  
>
>>The question is what is the best concurrent data structure
>>for this job.
>>
>>    
>>
>
>I'm looking at the source at https://swingworker.dev.java.net/
>  
>
it seems to be the same code.

>Is this sufficiently current?
>  
>
>If so, SwingWorker enqueues AccumulativeRunnable instances on the AWT
>EventQueue by way of the SwingUtilities.invokeLater() method.
>
>AccumulativeRunnable holds the accumulated arguments in an
>ArrayList<T> and synchronizes internally.  So there's no need to pass
>a thread-safe data structure to the process() method -- because
>thread-safety is already handled by the SwingWorker internals.
>
>I interpret the details as:
>
>When the submitted runnable is executed on the event thread, it grabs
>the existing elements from the internal ArrayList and calls the
>run(T... args) method.  These elements are grabbed by the flush()
>method.  The elements were added by the add() method.  Both methods
>are synchronized.  In this way one or more process(V... chunks) calls
>on the worker thread translate into a single run(T... args) call on
>the event thread.
>  
>
yes

>Are you proposing removing the synchronization from
>AccumulativeRunnable and replacing it with some kind of thread-safe
>collection?
>  
>
Yes.
The bug is due to the fact that there is no way to generate an array of 
T without
explicitly indicate the class of T.

One way to correct the bug is to change the signature of the run method
to run(List<T> args) or run(Queue<T> args).
In this case, its perhaps interresting to use a thread safe collection.

In perhaps it's a bad idea, i don't know, that why i ask for experts.

Rémi Forax





More information about the Concurrency-interest mailing list