Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement a queue of runnables
    primarykey
    data
    text
    <p>I am trying to implement a queue of runnables to be executed one after another(meaning the next in the queue will execute after the other has completed) during an asynchronous task. I wrote a Manager to manage these runnables and task that is a runnable itself. I then get the first task in the Asynchronous task and run it in hopes that it will run through the queue, however It just ends up running the first runnable twice. Can anyone help me with the code I have been working with or point me to an example that may be of some help? </p> <pre><code>public class ConnectionManager { public static final int MAX_CONNECTIONS = 15; private ArrayList&lt;Runnable&gt; active = new ArrayList&lt;Runnable&gt;(); private ArrayList&lt;Runnable&gt; queue = new ArrayList&lt;Runnable&gt;(); private static ConnectionManager instance; public static ConnectionManager getInstance() { if (instance == null) instance = new ConnectionManager(); return instance; } public void push(Runnable runnable) { queue.add(runnable); if (active.size() &lt; MAX_CONNECTIONS) startNext(); } private void startNext() { if (!queue.isEmpty()) { Runnable next = queue.get(0); queue.remove(0); active.add(next); Thread thread = new Thread(next); thread.start(); } } public void didComplete(Runnable runnable) { active.remove(runnable); startNext(); } } public class Task implements Runnable { Context con; String xmlFile; File taskFile; String Id; public void create(Context context, String xml, File task, String id) { this.con = context; this.xmlFile = xml; this.taskFile = task; this.Id = id; ConnectionManager.getInstance().push(this); } @Override public void run() { User.SendTask(con, xmlFile, taskFile, Id); ConnectionManager.getInstance().didComplete(this); } </code></pre>
    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.
 

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