Open source threadpool lib

天大地大妈咪最大 提交于 2019-12-08 13:10:36
andersoj

How about j.u.c.ThreadPoolExecutor? Properly wrapped and coupled with judicious use of Future it should meet your stated specs. If you have specific "block this thread until the following task set completes" behavior, you may also make use of a CompletionService.

As another answerer pointed out, you could also jump all the way to using Quartz Scheduler, if you really need a fully-fleshed-out task scheduling service. It sounds like that's overkill for your problem, but you didn't give specifics. If this is the path you take, there's a lot of good Q+A on Quartz Scheduler here at SO.

Check out ScheduledExecutors, run beepForAnHour once after 10 seconds, then every 10 seconds thereafter:

 import static java.util.concurrent.TimeUnit.*;
 class BeeperControl {
    private final ScheduledExecutorService scheduler =
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle =
            scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { beeperHandle.cancel(true); }
            }, 60 * 60, SECONDS);
    }
 }

From:

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html

Sounds like a job for Quartz to me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!