访问使用 java.util.Timer 计划的正在运行的任务

发布于 2024-08-31 09:17:22 字数 1419 浏览 2 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霊感 2024-09-07 09:17:22

不要使用计时器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 avoid Timer.

十雾 2024-09-07 09:17:22

在 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 inner Worker class, as well as making sure that Worker'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 single Timer. As an alternative, use ScheduledThreadPoolExecutor, where you have a pool of threads of a size you choose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文