创建一个新的事件处理程序和源代码
我有一个用户界面(UI)类。它创建了一些线程(让我们称之为T)来完成一些工作。我希望我的UI类在T完成工作时得到通知。 我想我需要在UI类(onClick()等)中创建一个事件处理程序,并从T触发它。 问题:这可能吗?怎么样 ? //要清楚,UI类已经有一些事件处理程序,它们是由我没写的函数触发的。像onClick()等。创建一个新的事件处理程序和源代码
回答:
这是一个相当普遍的要求,因为您通常希望尽可能少地在UI线程上进行操作。
如果您正在使用秋千,请看SwingWorker
课程。如果你不使用秋千,你可能想看看ExecutorService
和FutureTask
。
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);
}
}
以上是 创建一个新的事件处理程序和源代码 的全部内容, 来源链接: utcz.com/qa/266878.html