Если вы хотите, чтобы другой поток выполнил задачу, вы можете использовать ExecutorService и предоставить ему то, что вы хотите сделать. В приведенном здесь примере вы можете просто выполнить в run () то, что сделает второй поток.

ользую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);
    }
}

Как приостановить executorService, пока очередь, заполненная задачей, не будет очищена.

Ответы на вопрос(1)

Ваш ответ на вопрос