简体   繁体   English

Java从队列中检索所有元素,直到它为空

[英]Java retrieve all the elements from a queue until it is empty

I want to implement a Queue of elements in android/java such that, 我想在android / java中实现元素队列

  1. When I insert an element in that Queue, that element will start executing immediately if that is the only element in the Queue. 当我在该队列中插入一个元素时,如果该元素是队列中的唯一元素,则该元素将立即开始执行。
  2. If there are other elements in the Queue, they get executed sequentially one after the other. 如果队列中还有其他元素,则将它们依次依次执行。
  3. Once all the elements are executed the Queue waits for a new element to be inserted. 一旦所有元素都执行完,队列就等待插入新元素。 Once the element is inserted, it will again start the execution. 插入元素后,它将再次开始执行。

What could be the best Collection to use for this scenario? 在这种情况下,最好的集合是什么? Are there any default callbacks any collection provides which suffice condition 3? 有任何集合提供满足条件3的默认回调吗?

One implementation I can think of is writing a method in my Queue impelementation which is something like 我能想到的一个实现是在Queue实现中编写一种方法,类似于

public static void executeNext() {
    Executor element = getFirst();
    if (element != null && element.doneExecuting()) {
        pop();
    }

    Executor next = getFirst();
    next.execute();
}

which will be called from my Executor class object (which is an element of my Queue) once the execution finishes 执行完成后,将从我的Executor类对象(这是我的Queue的一个元素)中调用该对象

public void onExceutionComplete(){
    ExecutionQueue.getInstance().executeNext();
}

What you need is described in the JavaDoc . 您需要的内容在JavaDoc中进行了描述。 There is an implementation called SerialExecutor which might fit your problem. 有一个叫做SerialExecutor的实现可能适合您的问题。

Why dont you try to implement a listener, which will keep listening to the queue. 您为什么不尝试实现一个侦听器,它将继续侦听队列。

This will make sure as soon as new entries are in there, they would start getting executed. 这样可以确保一旦有新条目进入,它们就会开始执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM