0

タスクを同時に実行する 3 つのスレッドが必要であり、一定期間にわたってスケジュールされたタスクを実行するスレッドも必要です (10 秒ごとに実行されます)。しかし、3 つのスレッドが終了したら、スケジュールされたタスクを 1 回だけ実行してから、このスレッドを終了させたいと考えています。これらのスレッドを実装する最良の方法は何ですか? ExecutorService インターフェースはこれに適していますか。

4

2 に答える 2

3

ここに私が書いたばかりの例があります:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Example {

    public static void main(String[] args) throws InterruptedException {
        ScheduledFuture future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new MyScheduledTask(), 0, 10, TimeUnit.SECONDS);
        ExecutorService executor = Executors.newFixedThreadPool(3); // pool composed of 3 threads
        for (int i = 0; i < 3; i++) {
            // for the example assuming that the 3 threads execute the same task.
            executor.execute(new AnyTask()); 
        }
        // This will make the executor accept no new threads
        // and finish all existing threads in the queue
        executor.shutdown();
        // expect current thread to wait for the ending of the 3 threads
        executor.awaitTermination(Long.MAX_VALUE, TimeUnit.TimeUnit.NANOSECONDS); 
        future.cancel(false); // to exit properly the scheduled task
        // reprocessing the scheduled task only one time as expected
        new Thread(new ScheduledTask()).start(); 

    }


}

class MyScheduledTask implements Runnable {
    @Override
    public void run() {
        //Process scheduled task here
    }
}

class AnyTask implements Runnable {
    @Override
    public void run() {
        //Process job of threads here
    }
}
于 2012-10-24T01:41:02.183 に答える
1

これは、タスクを次々に実行する定期的なタスクスケジューラの例です。タスクスケジューラは一連のタスクを受け取り、別のスレッドで指定された間隔の後に各タスクを終了します。20秒後、メインスレッドはスケジューラーをシャットダウンし、待機中のすべてのタスクを停止します。

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Example {

    class ScheduledRepeatingTask implements Runnable {
        final ScheduledExecutorService scheduler = Executors
                .newScheduledThreadPool(1);

        List<Runnable> taskCollection;
        Iterator<Runnable> taskIterator;

        Runnable getCancelRunnable(final ScheduledFuture<?> future) {

            Runnable cancelFuture = new Runnable() {
                public void run() {
                    future.cancel(true);
                }
            };
            return cancelFuture;
        }

        public ScheduledRepeatingTask(Runnable[] tasks) {
            super();
            taskCollection = Arrays.asList(tasks);
            taskIterator = taskCollection.iterator();
        }

        public void shutdownScheduler() throws InterruptedException {
            scheduler.shutdown();
            boolean execTerminated = scheduler.awaitTermination(5,
                    TimeUnit.SECONDS);

            if (!execTerminated) {
                scheduler.shutdownNow();
            }
        }

        public boolean isShutdown(){            
            return scheduler.isShutdown();
        }

        public void scheduleRepeatingTask(ScheduledFuture<?> future,
                ScheduledFuture<?> futureCancel) {

            try {
                futureCancel.get();
                future.get();

            } catch (CancellationException e) {
                System.out.println("cancelled.");
            } catch (ExecutionException e) {
                Throwable exp = e.getCause();
                if (exp instanceof RuntimeException) {
                    System.out.println("failed.");
                    RuntimeException rt = (RuntimeException) exp;
                    throw rt;
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                if (!scheduler.isShutdown() && taskIterator.hasNext()) {
                    future = scheduler.scheduleAtFixedRate(taskIterator.next(),
                            2, 2, TimeUnit.SECONDS);
                    futureCancel = scheduler.schedule(
                            getCancelRunnable(future), 5, TimeUnit.SECONDS);
                    scheduleRepeatingTask(future, futureCancel);

                }
            }
        }

        @Override
        public void run() {
            if (!scheduler.isShutdown() && taskIterator.hasNext()) {
                ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(
                        taskIterator.next(), 2, 2, TimeUnit.SECONDS);
                ScheduledFuture<?> futureCancel = scheduler.schedule(
                        getCancelRunnable(future), 5, TimeUnit.SECONDS);
                scheduleRepeatingTask(future, futureCancel);
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
        Example example = new Example();
        ExecutorService executor = Executors.newCachedThreadPool(); 

        Runnable[] tasks = { new Task1(), new Task2(), new Task1() };

        ScheduledRepeatingTask scheduledRepeatingTask = example.new ScheduledRepeatingTask(tasks);

        executor.execute(scheduledRepeatingTask);

        Thread.sleep(20000);

        scheduledRepeatingTask.shutdownScheduler();

        executor.shutdown();

    }

}

class Task1 implements Runnable {
    @Override
    public void run() {
        System.out.println("Task 1");
    }
}

class Task2 implements Runnable {
    @Override
    public void run() {
        System.out.println("Task 2");
    }
}
于 2012-10-24T08:53:03.500 に答える