Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>But I need to guarantee that all threads that are created are run in sequence so no frames are dropped.</p> </blockquote> <p>Do you mean this as you worded it here? If so, then you can't really multithread this at all, as I understand that you can't start processing frame 2 until frame 1 has finished. At which point, you may as well process the frames sequentially and ignore threading.</p> <p>Alternatively if you mean something else, such as that the frames can be <em>processed</em> independently but need to be <em>collated</em> in sequence, then this could be doable.</p> <p>In any case - resorting to using "raw" threads is rarely required or beneficial. As others have pointed out, use the higher-level concurrency utilities (in this case a ThreadPoolExecutor would be perfect) to oversee this.</p> <p>It sounds like a <code>Runnable</code> isn't quite the right choice either, as that implies you're returning the "result" of the processing by mutating some global variable. Instead it may be better to turn this processing into a <code>Callable</code> and <em>return</em> the result. This would likely remove thread-safety issues, might allow different frames to be processed at once with less problems and would allow you to defer the collation of each result until whatever point you deemed fit.</p> <p>If you wanted to go this way, you could do something like the following:</p> <pre><code>// Create a thread pool with the given concurrency level ExecutorService executor = Executors.newFixedThreadPool(Runtime.availableProcessors); // Submit all tasks to the pool, storing the futures for further reference // The ? here should be the class of object returned by your Callables List&lt;Future&lt;?&gt;&gt; futures = new ArrayList&lt;Future&lt;?&gt;&gt;(NUM_FRAMES); for (int i = 0; i &lt; NUM_FRAMES; i++) { futures.add(executor.submit(createCallableForFrame(i))); } // Combine results using future.get() // e.g. do something with frames 2 and 3: mergeFrames(futures.get(2).get(), futures.get(3).get()); // In practice you'd probably iterate through the Futures but it's your call! </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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