使用Java线程获取和发送消息 [英] Get and send messages with Java Threads
问题描述
我想创建一个线程,该线程运行,使用我提供的数据计算某些内容,并返回一些值或一个对象.该线程是Swing GUI的一部分.
I want to make a thread, which runs, computes something with the data i give it, and returns a few values, or an object. The thread is a part of a Swing GUI.
我的问题:如何创建一个在线程创建并返回对象(或想要返回的对象)时运行的方法?
My question: How can I make a method that runs when I make the thread, and returns an object (or whatever I want it to return)?
我的代码:
private void nextTurn () {
// do something
if (turn == white) {
try {
Engine e = new Engine(); // Engine is implemented by runnable
e.start();
Move m = e.getBestMove (board);
// thread should work, next code should be excecuted immediately
}
catch (Exception e) {}
}
// end of Main class
}
这是我第一次使用Threads,我知道您应该尽可能避免使用它们,但是这次我的GUI需要它. Oracle网站上关于Threads的信息并没有帮助我.我可以制作一个具有不确定运行的多个线程的程序,但不能使其与函数一起使用.
This is the first time I am working with Threads, and I know you should avoid them if possible, but I need it this time for my GUI. The info on the Oracle site on Threads did not help me out. I am able to make a program with multiple Threads that runs indefinately, but I can't make it work with functions.
推荐答案
由于这是带有Swing GUI的,请考虑使用创建后台线程的SwingWorker
对象(所有代码均在doInBackground
方法中运行),然后可以返回最终结果和/或中期结果.有关如何使用它的信息已在此处的教程中得到了很好的记录:
Since this is with a Swing GUI, consider using a SwingWorker
object which creates a background thread (all the code run in the doInBackground
method), and then can return a final result and/or interim results. Information on how to use this is well documented in the tutorials here:
SwingWorkers具有属性更改支持,因此将允许侦听器通过PropertyChangeListener观察其状态(作为SwingWorker.StateValue).这是您的程序可以确定线程已完成其处理,获取返回结果并从那里进行的一种方法.
SwingWorkers have property change support and thus will allow listeners to observe its state (as a SwingWorker.StateValue) via a PropertyChangeListener. This is one way your program can determine that the thread has completed its processing, get the returned result and go from there.
在不相关的注释上,这不是您的生产代码吗?:
On an unrelated note, this isn't in your production code is it?:
catch (Exception e) {}
如果是这样,您可能会想解决此问题,因为被忽略的异常会在很长一段时间内咬住您.
If so, you will likely want to fix this as ignored exceptions can bite you in the tail big time.
例如
if (turn == white) {
try {
final SwingWorker<Move, Void> mySwingWorker = new SwingWorker<Move, Void>() {
@Override
protected Move doInBackground() throws Exception {
Engine e = new Engine(); // Engine is implemented by runnable
e.start();
Move m = e.getBestMove(board);
return m;
}
};
mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (StateValue.DONE == mySwingWorker.getState()) {
try {
Move m = mySwingWorker.get();
// TODO: insert code to run on the EDT after move determined
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
});
mySwingWorker.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
这篇关于使用Java线程获取和发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!