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

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

问题描述

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

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.

在我看来,要通知的各种事件不同类别的最好的办法是有一个定义了所有类型的事件表明该类的一个 IClassNameHereWatcher 接口出版的事件将需要通知,然后需要听会实现这个接口和自身注册为一个监听器每个类。我不是很肯定的事情是如何使这个异步的。这里是什么约我指的是:

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.

推荐答案

我会建议从java.util.concurrent包的执行人。
这是怎样的回调异步的<一个被处理href=\"http://github.com/schildmeijer/raptor/blob/master/src/main/java/org/raptorframework/raptor/AsyncFileMonitor.java\"相对=nofollow>猛禽框架:

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。这个调度再次发生。

NB. this scheduling is recurring.

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

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