Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Everyone seems to be asserting you're using the <code>List</code> to keep a queue of Tasks, but I don't actually see that in your question. But if it is, or if the manipulations to the list are otherwise independent — that is, if you are simply adding to or removing from the list as opposed to say, scanning the list and removing some items from the middle as a part of the job — then you should consider using a <code>BlockingQueue</code> or <code>QueueDeque</code> instead of a <code>List</code> and simply leveraging the <code>java.util.concurrent</code> package. These types do not require external lock management.</p> <p>If you require in earnest a <code>List</code> which is concurrently accessed by the each job, where the reads and writes to the list not independent, I would encapsulate the part of the processing which does this manipulation in a singleton, and use an exclusive lock to have each thread make use of the list. For instance, if your list contains some sort of aggregate statistics which are only a part of the process execution, then I would have my job be one class and a singleton aggregate statistics be a separate job.</p> <pre><code>class AggregateStatistics { private static final AggregateStatistics aggregateStatistics = new AggregateStatistics(); public static AggregateStatistics getAggregateStatistics () { return aggregateStatistics; } private List list = new ArrayList (); private Lock lock = new ReentrantLock(); public void updateAggregates (...) { lock.lock(); try { /* Mutation of the list */ } finally { lock.unlock(); } } } </code></pre> <p>Then have your task enter this portion of the job by accessing the singleton and calling the method on it which is managed with a lock.</p> <p><em>Never</em> pass a collection which into a concurrent environment, it will only cause you problems. You can always pass around an immutable "wrapper" though if it's really suitable, by using <code>java.util.Collections.unmodifiableList(List)</code> and similar methods.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload