Я использую ScheduledExecutorService
для выполнения задачи, которая вызывает службу с фиксированной скоростью. Сервис может вернуть некоторые данные задаче. Задача хранит данные в очереди. Некоторые другие потоки медленно выбирают элементы из очереди
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class EverlastingThread implements Runnable {
private ScheduledExecutorService executorService;
private int time;
private TimeUnit timeUnit;
private BlockingQueue<String> queue = new LinkedBlockingQueue<String>(500);
public EverlastingThread(ScheduledExecutorService executorService, int time, TimeUnit timeUnit) {
this.executorService = executorService;
this.time = time;
this.timeUnit = timeUnit;
}
public void run() {
// call the service. if Service returns any data put it an the queue
queue.add("task");
}
public void callService() throws Exception {
// while queue has stuff dont exucute???????????
executorService.scheduleAtFixedRate(this, 0, time, timeUnit);
}
}
Как приостановить выполнение ExectorService до тех пор, пока очередь, заполненная задачей, не будет очищена.