创建新的事件处理程序和源



我有一个用户界面(UI)类。它创建了一些线程(让我们称之为T)来做一些工作。我希望我的UI类在T完成工作时得到通知。我想我需要在UI类中创建一个事件处理程序(在onClick()等中),并从T触发它。问题:这可能吗?怎样//需要明确的是,UI类已经有一些事件处理程序,它们是由我没有编写的函数触发的。如onClick()等。

这是一个相当常见的需求,因为您通常希望在UI线程上尽可能少地执行。

如果您正在使用swing,请查看SwingWorker类。如果你没有使用swing,你可能想看看ExecutorServiceFutureTask

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class Futures {
    public static void main(String[] args) {
        UI ui = new UI();
        FutureHandle<String> handle = new FutureHandle<String>(new BigJob());
        FutureHandle<String> handle2 = new FutureHandle<String>(new BigJob());
        ui.doUIStuff("Things can happen on the UI thread");
        ui.startHeavyLiftingJob(handle);
        ui.doUIStuff("I've got a big job running, but I'm still responsive");
        ui.startHeavyLiftingJob(handle2);
    }

    /**
     * Your UI class. Don't want to do anything big
     * on the UI's thread.
     */
    static class UI implements Listener<String> {
        private ExecutorService threadPool = Executors.newFixedThreadPool(5);
        public void doUIStuff(String msg) {
            System.out.println(msg);
        }
        public void startHeavyLiftingJob(FutureHandle<String> handle) {
            System.out.println("Starting background task");
            handle.setListener(this);
            threadPool.execute(handle);
        }
        public void callback(String result) {
            System.out.println("Ooh, result ready: " + result);
        }
    }

    /**
     * A handle on a future which makes a callback to a listener
     * when the callable task is done.
     */
    static class FutureHandle<V> extends FutureTask<V> {
        private Listener<V> listener;
        public FutureHandle(Callable<V> callable) {
            super(callable);
        }
        @Override
        protected void done() {
            try {
                listener.callback(get());
            } catch (InterruptedException e) {
                //handle execution getting interrupted
            } catch (ExecutionException e) {
                //handle error in execution
            }
        }
        public void setListener(Listener<V> listener) {
            this.listener = listener;
        }
    }
    /**
     * Class that represents something you don't want to do on the UI thread.
     */
    static class BigJob implements Callable<String> {
        public String call() throws Exception {
            Thread.sleep(2000);
            return "big job has finished";
        }
    }

    interface Listener<V> {
        public void callback(V result);
    }
}

相关内容

  • 没有找到相关文章

最新更新