我正在尝试创建一个每 5 秒运行一次的任务,但是当我上课时我该如何执行此操作
public class BlueSlime extends Slime implements Shootable, TimerTask
已经扩展了一个类?
我可以使用其他类型的任务吗?
最佳答案
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.
关于java - 当我的类已经扩展了一个类时,如何实现 TimerTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272089/