访问使用 java.util.Timer 计划的正在运行的任务
我正在开发一个 Java 项目,在该项目中我创建了一个如下所示的类(缩写版本):
public class Daemon {
private static Timer[] timerarray=null;
private static Daemon instance=null;
protected Daemon() {
ArrayList<Timer> timers = new ArrayList<Timer>();
Timer t = new Timer("My application");
t.schedule(new Worker(), 10000,30000);
timers.add(t);
//...
timerarray = timers.toArray(new Timer[]{});
}
public static Daemon getInstance() {
if(instance==null) instance=new Daemon();
return instance;
}
public SomeClass getSomeValueFromWorker(int i) {
if(timerarray==null || i>=timerarray.length) return null;
/*
* **HOW TO ACCESS THE Worker INSTANCE IN THE Timer ARRAY HERE!? **
*/
return theValue;
}
/////////////////////////////////////////////
private class Worker extends TimerTask {
public Worker() {}
public void run() {
// do some work
}
public SomeReturnClass someMethod(SomeType someParameter) {
//
return something;
}
}
/////////////////////////////////////////////
}
我启动该类,例如通过调用 daemon.getInstance();
。
但是,我希望有某种方法来访问正在运行的任务对象的方法(例如,用于监视对象的状态)。 Java 类 java.util.Timer 似乎没有提供访问正在运行的对象的方法,它只是调度扩展 TimerTask 的对象实例。
有没有办法访问定时器中实例化的“正在运行”的对象?我是否必须使用适当的方法对 Timer 类进行子类化才能以某种方式访问该实例(不知何故,这“感觉”很奇怪)? 我想以前可能有人这样做过......我在哪里可以找到这个“程序”的例子?
预先感谢您的反馈。
I'm working on a Java project where I have created a class that looks like this (abridged version):
public class Daemon {
private static Timer[] timerarray=null;
private static Daemon instance=null;
protected Daemon() {
ArrayList<Timer> timers = new ArrayList<Timer>();
Timer t = new Timer("My application");
t.schedule(new Worker(), 10000,30000);
timers.add(t);
//...
timerarray = timers.toArray(new Timer[]{});
}
public static Daemon getInstance() {
if(instance==null) instance=new Daemon();
return instance;
}
public SomeClass getSomeValueFromWorker(int i) {
if(timerarray==null || i>=timerarray.length) return null;
/*
* **HOW TO ACCESS THE Worker INSTANCE IN THE Timer ARRAY HERE!? **
*/
return theValue;
}
/////////////////////////////////////////////
private class Worker extends TimerTask {
public Worker() {}
public void run() {
// do some work
}
public SomeReturnClass someMethod(SomeType someParameter) {
//
return something;
}
}
/////////////////////////////////////////////
}
I start this class, e.g. by invoking daemon.getInstance();
.
However, I'd like to have some way to access the running task objects' methods (for example, for monitoring the objects' state).
The Java class java.util.Timer
does not seem to provide the means to access the running object, it just schedules the object instance extending TimerTask
.
Are there ways to access the "running" object instanciated within a Timer
? Do I have to subclass the Timer
class with the appropriate methods to somehow access the instance (this "feels" strange, somehow)?
I suppose someone might have done this before ... where can I find examples of this "procedure"?
Thank you in advance for your feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用
计时器
。 ScheduledExecutorService 实现是更好,并通过一些额外的工作提供您想要的功能。请参阅 409932 了解避免使用Timer
的一些原因。Don't use
Timer
. The ScheduledExecutorService implementations are much better and provide the functionality you want with some extra work. See 409932 for some reasons why to avoidTimer
.在 Daemon 构造函数中,您可以将 Worker 的实例收集到某种集合中,并为该集合添加 getter。这需要增加对内部
Worker
类的访问级别,并确保Worker
的方法是线程安全的。另一件事:为什么要为每个作业创建一个新的计时器?如果您的作业完成得很快,并且您没有太多作业,请考虑将所有作业注册到一个
Timer
中。作为替代方案,请使用 ScheduledThreadPoolExecutor ,其中有一个您选择大小的线程池。In the Daemon constructor, you can collect the instances of
Worker
into some kind of collection, and add a getter for that collection. This requires increasing the access level to the innerWorker
class, as well as making sure thatWorker
's methods are thread safe.Another thing: Why do you create a new
Timer
per job? if your jobs finish quickly, and you don't have too many of them, consider registering all of them in a singleTimer
. As an alternative, use ScheduledThreadPoolExecutor, where you have a pool of threads of a size you choose.