这个 java .execute() 方法调用是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10753706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this java .execute() method call mean?
提问by yoonsi
I was doing some reading through the sun java tutorials, and I came across this page here:
我正在阅读 sun java 教程,我在这里看到了这个页面:
Under the heading, "Threads in Applets"I found this piece of code:
在标题“小程序中的线程”下,我找到了这段代码:
//Background task for loading images.
SwingWorker worker = (new SwingWorker<ImageIcon[], Object>() {
public ImageIcon[] doInBackground() {
final ImageIcon[] innerImgs = new ImageIcon[nimgs];
...//Load all the images...
return imgs;
}
public void done() {
//Remove the "Loading images" label.
animator.removeAll();
loopslot = -1;
try {
imgs = get();
} ...//Handle possible exceptions
}
}).execute();
}
First up I'm newish, so I'm sorry if this is a stupid question. However I've never heard of that ".excecute()". I don't understand it, and I'm unable to find anything about it from google. I see this here is... an anonymous inner class? (Please correct me) and it is starting a thread to load in images. I thought that the run() method is invoked with a call to start()? Please help me clear this confusion.
首先,我是新手,所以如果这是一个愚蠢的问题,我很抱歉。但是我从来没有听说过“.execute()”。我不明白,我无法从谷歌找到任何关于它的信息。我看到这是...一个匿名内部类?(请纠正我)它正在启动一个线程来加载图像。我认为 run() 方法是通过调用 start() 来调用的?请帮我清除这个困惑。
回答by T.J. Crowder
execute
is a method of SwingWorker
. What you're seeing there is an anonymous classbeing instantiated and having its execute
method called immediately.
execute
是一种方法SwingWorker
。你看到的是一个匿名类被实例化并execute
立即调用它的方法。
I have to admit I'm a bit surprised that code compiles, though, because it seems to be assigning the result of execute
to the worker
variable, and the documentation tells us that execute
is a void
function.
我不得不承认我有点惊讶的是代码编译,但是,因为它似乎是分配的结果execute
的worker
变量,并且该文档告诉我们,execute
是一个void
功能。
If we deconstruct that code a bit, it can be clearer. First, we create an anonymous class extending SwingWorker
and create an instance of it, all at the same time (this is the big bit in parentheses):
如果我们稍微解构一下代码,它会更清晰。首先,我们同时创建一个匿名类扩展SwingWorker
并创建它的一个实例(这是括号中的重要部分):
SwingWorker tmp = new SwingWorker<ImageIcon[], Object>() {
public ImageIcon[] doInBackground() {
final ImageIcon[] innerImgs = new ImageIcon[nimgs];
...//Load all the images...
return imgs;
}
public void done() {
//Remove the "Loading images" label.
animator.removeAll();
loopslot = -1;
try {
imgs = get();
} ...//Handle possible exceptions
}
};
Then we call execute
and assign the result to worker
(which is the bit that, it seems to me, shouldn't compile):
然后我们调用execute
并将结果分配给worker
(在我看来,这是不应该编译的部分):
SwingWorker worker = tmp.execute();
Update: And indeed, I tried it and it doesn'tcompile. So not great example code. This would compile:
更新:确实,我试过了,但无法编译。所以不是很好的示例代码。这将编译:
SwingWorker worker = new SwingWorker<ImageIcon[], Object>() {
public ImageIcon[] doInBackground() {
final ImageIcon[] innerImgs = new ImageIcon[nimgs];
...//Load all the images...
return imgs;
}
public void done() {
//Remove the "Loading images" label.
animator.removeAll();
loopslot = -1;
try {
imgs = get();
} ...//Handle possible exceptions
}
};
worker.execute();
回答by Stephen C
The .execute()
is calling the execute method on an instance of an anonymous class; i.e. the object created by new SwingWorker<ImageIcon[], Object>(){...}
. (It is a class that extends the SwingWorker
class.)
该.execute()
呼吁匿名类的一个实例execute方法; 即创建的对象new SwingWorker<ImageIcon[], Object>(){...}
。(它是一个扩展SwingWorker
类的类。)
According to the javadoc, the execute
method schedules the task represented by the instance to be executed on an existing worker thread. The lifecycle of the worker thread (e.g. creation, starting, etcetera) is taken care of by Swing infrastructure.
根据 javadoc,该execute
方法调度由实例表示的任务在现有工作线程上执行。工作线程的生命周期(例如创建、启动等)由 Swing 基础设施负责。