我的代码:private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);public void beepForAnHour(){ scheduler.scheduleWithFixedDelay(new ExampleThread(), 1L, 1L, SECONDS);}private static class ExampleThread implements Runnable{ @Override public void run() { System.out.println("1:"+System.currentTimeMillis()); try { Thread.sleep(5000L); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println("2:"+System.currentTimeMillis()); }}结果的示例将是:1:14165990269882:14165990319881:14165990329892:14165990379891:1416599038990我不知道应该在代码中进行哪些更改,因此下一个ExampleThread将在上一个Thread开始后的1秒后启动,而不是在其结束后开始。睡觉的事情只是显示我的问题的一个例子。所以我希望看到的结果是:1:14165990269881:14165990279881:14165990289881:14165990299881:14165990309882:14165990309881:14165990319882:14165990319881:14165990329882:1416599033988 最佳答案 您不能使用ScheduledExecutorService执行此操作。 scheduleWithFixedDelay的文档说: 创建并执行一个周期性操作,该操作将在给定的初始延迟后首先启用,然后在一个执行的终止与下一个执行的开始之间具有给定的延迟。如果该任务的任何执行遇到异常,则将禁止后续执行。否则,任务将仅通过取消或终止执行程序而终止。(我的重点)对于scheduleAtFixedRate,文档说: 创建并执行一个周期性操作,该操作将在给定的初始延迟后首先启用,然后在给定的时间段内启用;也就是说执行将在initialDelay然后initialDelay+period,然后initialDelay + 2 * period等之后开始。如果该任务的任何执行遇到异常,则将禁止后续执行。否则,任务将仅通过取消或终止执行程序而终止。如果此任务的任何执行花费的时间超过其周期,则后续执行可能会开始得较晚,但不会同时执行。此类旨在防止并发执行其任务。但是,您可以将调度程序用于其唯一任务是运行ExampleThread的线程:private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);public void beepForAnHour(){ scheduler.scheduleWithFixedDelay(new ThreadMaker(), 1L, 1L, TimeUnit.SECONDS);}private static class ThreadMaker implements Runnable{ @Override public void run() { Thread realThread = new Thread(new ExampleThread()); realThread.start(); }}private static class ExampleThread implements Runnable{ @Override public void run() { System.out.println("1:"+System.currentTimeMillis()); try { Thread.sleep(5000L); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println("2:"+System.currentTimeMillis()); }}这样,您的计划任务仅运行创建新线程所需的一秒钟时间,因此调度程序将大约每秒运行一次(如果使用scheduleAtFixedRate,它将更加精确)。而且“真实”线程可以自由地并发运行。当然,如果您这样做,将有多个ExampleThread对象,并且还需要弄清楚如何与创建的线程进行通信以取消它们,因为它们不能直接从由ScheduledFuture或scheduleWithFixedDelay返回的对象。