当我的 class 已经扩展了 class 时,如何实现 TimerTask?

How can I implement TimerTask when my class already extends a class?

我正在尝试每 5 秒创建一个到 运行 的任务,但是当我的 class

public class BlueSlime extends Slime implements Shootable, TimerTask

已经扩展了一个 class?

我可以使用其他类型的任务吗?

为什么任务应该知道它 运行 的频率?

  • 创建您的任务并使其实现 运行nable。
  • 创建或使用单独的任务计划程序或Executor
  • 运行 带有调度程序的任务。
  • 利润

一项任务应该描述运行的工作,而不是运行

的频率]

Java v 5+:

Timer 摘录:

Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.