gpt4 book ai didi

java - 我在理解并发文档时遇到问题

转载 作者:行者123 更新时间:2023-12-01 13:21:44 25 4
gpt4 key购买 nike

我正在查看并发文档,但我不太明白它们的含义:

In an applet, the GUI-creation task must be launched from the init method using invokeAndWait; otherwise, init may return before the GUI is created, which may cause problems for a web browser launching an applet. In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.'

-在 GUI 创建之前返回 init 有什么问题?- 为什么 GUI 创建通常是线程所做的最后一件事?

Tasks on the event dispatch thread must finish quickly; if they don't, unhandled events back up and the user interface becomes unresponsive.'

-怎样才能让它更快地完成?

http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TumbleItemProject/src/components/TumbleItem.java

-上例中的 EDT 在哪里?

SwingWorker worker = new SwingWorker<ImageIcon[], Void>() {
@Override
public ImageIcon[] doInBackground() {
final ImageIcon[] innerImgs = new ImageIcon[nimgs];
for (int i = 0; i < nimgs; i++) {
innerImgs[i] = loadImage(i+1);
}
return innerImgs;
}

@Override
public void done() {
//Remove the "Loading images" label.
animator.removeAll();
loopslot = -1;
try {
imgs = get();
} catch (InterruptedException ignore) {}
catch (java.util.concurrent.ExecutionException e) {
String why = null;
Throwable cause = e.getCause();
if (cause != null) {
why = cause.getMessage();
} else {
why = e.getMessage();
}
System.err.println("Error retrieving file: " + why);
}
}

};

-为什么这里初始化“worker”之后会覆盖几个方法,而不仅仅是“;”?我以前从未见过这种符号......- 除“doInBackGround()”方法之外的所有方法都在事件调度线程下执行吗?

'SwingWorker的所有具体子类都实现doInBackground;完成的实现是可选的。'

-在代码示例中,我没有看到 SwingWorker 的子类,除非 new SwingWorker <>() 算作子类?

'从事件调度线程调用 get 的重载时要小心;在 get 返回之前,不会处理任何 GUI 事件,并且 GUI 被“卡住”。除非您确信后台任务已完成或接近完成,否则不要调用不带参数的 get。”

-您将如何以非 EDT 方式使用 get() ?

如果有些问题很明显,我深表歉意,感谢您抽出时间!

最佳答案

What is the problem with the init being returned before GUI-creation? -Why is the GUI-creation usually the last thing a thread does?

它就在文本中说了。 “这可能会给浏览器带来问题”。这可能是因为 init 的调用者需要该方法来创建 GUI 并为其安排 GUI 消息,如果没有创建,则调用者将失败。

How can you make it finish faster?

这并不是为了让线程运行得更快,而是如果您有一个长期任务,请在后台线程而不是事件(GUI)线程中执行它,以免使其卡住。

Where is the EDT in the above example?

它无处可去.. 小程序有一个 EDT,当您单击按钮或以其他方式与小程序交互时会使用它。我不太明白你的问题。

Why is initialisation of 'worker' here followed by the overwriting of several methods instead of just ';'? I have never seen this kind of notation before... -Are all methods that aren't the 'doInBackGround()' method ,executed under the event dispatch thread?

这是一个 anonymous class declaration SwitchWorker 类的。不,doInBackground 不在 EDT 上执行,而是在后台执行。不过,done 已安排在美国东部时间 (EDT)。请参阅http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html供引用。

In the code example ,I don't see a subclass for SwingWorker , unless new SwingWorker <>() , counts as a subclass?

确实如此。

How would you use get() in a non-EDT way ?

如文档所示:

get()

Waits if necessary for the computation to complete, and then retrieves its result.

因此,在后台方法完成之前不应调用它,以避免 GUI 卡住。

关于java - 我在理解并发文档时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com