Java中的异步事件调度 [英] Asynchronous Event Dispatch in Java

查看:191
本文介绍了Java中的异步事件调度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将C#程序移植到Java中,这使得代理人和代理的 BeginInvoke 方法大量使用来异步通知事件。以数据通信线程为例。它可能需要通知另一个工作线程其状态以及GUI。



在我看来,通知不同类的各种事件的最佳方法是拥有一个 IClassNameHereWatcher 接口,定义事件类发布事件需要通知的所有类型的事件,然后每个需要监听的类将实现此接口并将其注册为侦听器。我不太确定的是如何使这个异步。这里是我所指的:

  public interface IFrobWatcher {
void frobDidSomething();
void frobReceivedData(object data);
}

public class Frob实现Runnable {
列表< IFrobWatcher> watchers = new ArrayList< IFrobWatcher>();

private void run(){
while(true){
//长运行任务
if(blah)notifyWeDidSomething();
notifyOfData(someDataObject);
}
}

public void addWatcher(IFrobWatcher watcher){
watchers.Add(watcher);
}

private void notifyWeDidSomething(){
for(IFrobWatcher watcher:watchers){
watcher.frobDidSomething(); //如何使这个异步?
}
}

private void notifyOfData(object someDataObject){
for(IFrobWatcher watcher:watchers){
watcher.frobReceivedData(someDataObject); //如何使这个异步?
}
}

}

public class FrobWatcher实现IFrobWatcher {
private Frob frobToWatch;

public FrobWatcher(Frob frob){
frobToWatch = frob;
frobToWatch.addListener(this);
}

public void FrobDidSomething(){
System.out.println(Frob did something!);
}

public void FrobReceivedData(接收到对象){
System.out.println(Frob received:+ received.ToString());
}

public static void main(string [] args){
Frob f = new Frob();
FrobWatcher fw = new FrobWatcher(f);
(new Thread(f))。start();
}
}

这是一个相当简化的例子,但希望它传达我正在寻找的东西。如果有什么不清楚或没有意义,请评论,我会尝试澄清或修正这个例子。

解决方案

p>我将从java.util.concurrent包中推荐一个Executor。
这是在 Raptor框架

  scheduler = Executors.newSingleThreadScheduledExecutor(); 
scheduler.scheduleAtFixedRate(
filesPoller,0 / * initial delay * /,
checkInterval,
TimeUnit.MILLISECONDS
);

NB。这个安排是重复的。


I'm in the midst of porting a C# program over to Java that makes heavy use of delegates and the delegate's BeginInvoke method to notify of an event asynchronously. Take a data communication thread for example. It might have to notify another worker thread of its state as well as the GUI.

It seems to me that the best way to notify of the various events for different classes is to have an IClassNameHereWatcher interface that defines all of the types of events that the class "publishing" the event would need to notify about and then each class that needs to listen would implement this interface and register itself as a listener. The thing I'm not quite sure about is how to make this asynchronous. Here's approximately what I'm referring to:

public interface IFrobWatcher {
    void frobDidSomething();
    void frobReceivedData(object data);
}

public class Frob implements Runnable {
    List<IFrobWatcher> watchers = new ArrayList<IFrobWatcher>();

    private void run() {
        while (true) {
            // Long running task
            if (blah) notifyWeDidSomething();
            notifyOfData(someDataObject);
        }
    }

    public void addWatcher(IFrobWatcher watcher) {
        watchers.Add(watcher);
    }

    private void notifyWeDidSomething() {
        for (IFrobWatcher watcher : watchers) {
            watcher.frobDidSomething(); // How do I make this asynchronous?
        }
    }

    private void notifyOfData(object someDataObject) {
        for (IFrobWatcher watcher : watchers) {
            watcher.frobReceivedData(someDataObject); // How do I make this asynchronous?
        }
    }

}

public class FrobWatcher implements IFrobWatcher {
    private Frob frobToWatch;

    public FrobWatcher(Frob frob) {
        frobToWatch = frob;
        frobToWatch.addListener(this);
    }

    public void FrobDidSomething() {
        System.out.println("Frob did something!");
    }

    public void FrobReceivedData(object received) {
        System.out.println("Frob received: " + received.ToString());
    }

    public static void main(string[] args) {
        Frob f = new Frob();
        FrobWatcher fw = new FrobWatcher(f);
        (new Thread(f)).start();
    }
}

And this is a fairly simplified example, but hopefully it conveys what I'm looking for. If something isn't clear or doesn't make sense, please comment and I'll try to clarify or fix the example.

解决方案

I would recommend an Executor from the java.util.concurrent package. This is how asynchronous callbacks are handled in the Raptor framework:

scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(
    filesPoller, 0 /*initial delay*/,
    checkInterval,
    TimeUnit.MILLISECONDS
);

NB. this scheduling is recurring.

这篇关于Java中的异步事件调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆