Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Runnable Queue
    text
    copied!<p>I need peer reviews of the critical piece of code below.</p> <p>This class maintain a queue of runnable objets, and ensure they are executed sequentially, that is, start one new after the previous one is completed until there is no more task in the queue.</p> <p>I'm pretty sure it does, but I'm must be absolutely sure it behave as intented.</p> <p>Many thanks !</p> <pre><code>public final class RunnableQueue { private final ExecutorService m_executorService; private final Queue&lt;Runnable&gt; m_runnables; private final Runnable m_loop; public RunnableQueue(ExecutorService executorService) { m_executorService = executorService; m_runnables = new LinkedList&lt;Runnable&gt;(); m_loop = new Runnable() { public void run() { Runnable l_runnable = current(); while(l_runnable != null) { l_runnable.run(); l_runnable = next(); } } }; } private Runnable current() { synchronized (m_runnables) { return m_runnables.peek(); } } private Runnable next() { synchronized (m_runnables) { m_runnables.remove(); return m_runnables.peek(); } } public void enqueue(Runnable runnable) { if(runnable != null) { synchronized (m_runnables) { m_runnables.add(runnable); if(m_runnables.size() == 1) { m_executorService.execute(m_loop); } } } } } </code></pre> <p><strong>Edit</strong></p> <p>Basicaly, there will be hundreds of <code>RunnableQueue</code> instanciated using the same <code>ThreadPool</code> and each <code>Runnable</code> executed may add other <code>Runnable</code> in other <code>RunnableQueue</code>.</p> <p>So, new <code>Runnable</code> will be added to the <code>RunnableQueue</code> while it runs...</p>
 

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