如何实现可运行的队列 [英] How to implement a queue of runnables

查看:170
本文介绍了如何实现可运行的队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现可运行的队列中执行一个接一个(意为下一个队列后,其他将执行已完成)在异步任务。我写了一个经理来管理这些可运行和任务是可运行的本身。然后我得到了异步任务的第一个任务,因为它会通过队列运行希望运行它,但它只是结束了两次运行的第一个可运行。谁能帮我用code我一直在使用或指向我,可能是有所帮助的例子吗?

 公共类的ConnectionManager {

    公共静态最终诠释MAX_CONNECTIONS = 15;

    私人的ArrayList<可运行>积极=新的ArrayList<可运行>();
    私人的ArrayList<可运行>队列=新的ArrayList<可运行>();

    私有静态的ConnectionManager实例;

    公共静态的ConnectionManager的getInstance(){
        如果(例如== NULL)
            例如=新的ConnectionManager();
        返回实例;
    }

    公共无效的push(Runnable的可运行){
        queue.add(可运行);
        如果(active.size()&其中; MAX_CONNECTIONS)
            startNext();
    }

    私人无效startNext(){
        如果(!queue.isEmpty()){
            可运行的下一个= queue.get(0);
            queue.remove(0);
            active.add(下);

            线程线程=新主题(下);
            thread.start();
        }
    }

    公共无效didComplete(Runnable的可运行){
        active.remove(可运行);
        startNext();
    }
}

公共类任务实现Runnable {
    语境CON;
    字符串XMLFILE;
    文件taskFile;
    字符串ID;

    公共无效创建(上下文的背景下,XML字符串,文件任务,串号){
        this.con =背景;
        this.xmlFile = XML;
        this.taskFile =任务;
        this.Id = ID;
        。ConnectionManager.getInstance()推(本);
    }

    @覆盖
    公共无效的run(){
        User.SendTask(CON,XMLFILE,taskFile,身份证);

        。ConnectionManager.getInstance()didComplete(本);
    }
 

解决方案

有关如何使用Executors.newSingleThreadExecutor()? <一href="http://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor%28java.util.concurrent.ThreadFactory%29" rel="nofollow">http://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor%28java.util.concurrent.ThreadFactory%29

API说明解释说,此执行顺序执行的任务只有一个,具有无限的队列。我觉得这个人能满足您的要求。

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?

public class ConnectionManager {

    public static final int MAX_CONNECTIONS = 15;

    private ArrayList<Runnable> active = new ArrayList<Runnable>();
    private ArrayList<Runnable> queue = new ArrayList<Runnable>();

    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() < 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);
    }

解决方案

how about using Executors.newSingleThreadExecutor()? http://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor%28java.util.concurrent.ThreadFactory%29

API description explains that this executor execute only one task sequentially and has unlimited queue. I think this one can satisfies your requirement.

这篇关于如何实现可运行的队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆