java - JavaFX 应用程序线程的预期行为是什么
问题描述
EventHandler 实现 X 附加到 Pane 并侦听所有 MouseEvents。当然,X 有一个handle()
从 JavaFX 应用程序线程接收 MouseEvents 的方法。
窗格包含一个矩形。当 Pane 在 Rectangle 上收到 MouseEvent.MOUSE_CLICK 时,X 会做两件事:
从窗格中删除矩形,然后立即添加另一个(这可能会导致其他事件。
继续进行一些随意的处理
这是问题:
步骤 2 中的处理是否有望在 JavaFX 应用程序线程通过任何其他事件提交给 X之前完成?handle()
请注意,第 1 步可能会触发其他事件!
只是寻找是或否的回应。你的答案背后的推理也很好!
我应该补充一点,任何地方都没有涉及任何其他类型的线程,包括“任意处理”。
编辑:
示例代码
package bareBonesJavaFXBugExample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
/**
* An {@link Application} with one {@link Pane} containing one {@link Label}.
* The {@link Label} has a single {@link javafx.event.EventHandler},
* {@link LabelEventHandler} which processes all {@link MouseEvent}s the {@link Label}
* receives.
*
* To trigger the bug, run the application, then spend a second mouse over the
* little label in the upper left hand corner of the screen. You will see output to
* standard I/O. Then, click the label, which will then disppear. Check the I/O for
* Strings ending in debugCounter is 1.
*
* What that String means and how it proves that the JavaFX Application Thread has
* become reentrant is explained in the javadoc of {@link LabelEventHandler}.
*/
public class JavaFXAnomalyBareBonesApplication extends Application
{
public void start(Stage primaryStage)
{
Pane mainPane = new Pane();
mainPane.setMinHeight(800);
mainPane.setMinWidth(800);
Label label = new Label(" this is quite a bug !!!!");
LabelEventHandler labelEventHandler = new LabelEventHandler(mainPane, label);
label.addEventHandler(MouseEvent.ANY, labelEventHandler);
mainPane.getChildren().add(label);
Scene scene = new Scene(mainPane);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The entry point of application.
*
* @param args
* the input arguments
*/
public static void main(String[] args) {
launch(args);
}
}
这是它唯一的依赖项,EventListener 类。我包含了足够多的 javadoc 以使程序有意义。:
package bareBonesJavaFXBugExample;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import java.util.Collection;
import java.util.ConcurrentModificationException;
/**
* An {@link EventHandler} implementation for {@link MouseEvent}s.
* This implementation's {@link EventHandler#handle(Event)} shows the
* relevant debug information to standard output before and after removing
* the member {@link #label} from the {@link #pane}.
*
* <b>discussion</b><br></br>
* <p>
* Users should first satisfy themselves that the value of
* {@link LabelEventHandler#debugCounter} can only be non-zero, in fact 1
* (one) in the method {@link LabelEventHandler#showDebugInformation(String)}
* if the method {@link LabelEventHandler#handle(MouseEvent)} has been
* re-entered recursively, that is, before a previous invocation of
* {@link LabelEventHandler#handle(MouseEvent)} has returned.
* <p>
* Proof:
* 1) <code>debugCounter</code> starts at value 0 (zero).
* 2) <code>debugCounter</code> is only incremented once, by 1 (one), and that
* is after the first call to {@link LabelEventHandler#showDebugInformation(String)}
* has returned.
* 3) <code>debugCounter</code> is only decremented once, by 1 (one) and that
* is before the last call to {@link LabelEventHandler#showDebugInformation(String)}.
* 4) however, because <code>debugCounter</code> is a class variable
* (it's static), if handle() is recurvsively re-entered then it's
* value can be 1 (one) when the re-entrant
*
* Thread executes {@link LabelEventHandler#showDebugInformation(String)}
*
* End proof.
*
* The output of this method to standard I/O is volumnious but searching the
* output for the exact String "debugCounter is 1" will immediately show the
* {@link LabelEventHandler#handle(MouseEvent)} method to have been recursively
* entered.
*
* Some other possibilities other than the JavaFX Application Thread recursing
* into {@code handle()} need to be addressed.
* One is the fact that the compiler is free to reorder statements if it can
* prove that such a reordering would have no effect on the program's correctness.
*
* So somehow the compiler is reordering the increment/decrement of
* {@code debugCounter} and the calls to {@code showDebugInformation}.
* But this would alter the correctness of the program, so this cannot be the case,
* or the compiler is making an error.
*
* Another is the fact that I/O is not instantaneous and can appear to standard
* output later than it actually was executed.
* This is something often seen in debug stack traces, where the output is
* broken up or interleaved by the output of the stack trace even though the
* two sets of statments, i/o and stack trace i/o, were strictly ordered in execution.
* But this can't account for the value of {@code debugCounter}, so it can't
* be the reason "debugCounter is 1" appears in output.
*
* In fact we can make this recursive behaviour more obviously consequential
* to the correctness of the program. If {@code handle() } is being
* recursively re-entered, then we can force a
* {@link ConcurrentModificationException} on a {@link Collection}.
* If we try to invoke {@link Collection#add(Object)} to a {@link Collection}
* while it is being iterated through, then a {@link ConcurrentModificationException}
* will be thrown.
*
* If we re-write this program slightly to first add or remove to or from a
* {@link Collection} then iterate through that {@link Collection} within the
* scope of execution of {@code handle()}, <em>and</em> {@code handle()}
* is being recursively invoked, then we may see a {@link ConcurrentModificationException}.
*
* Two other instances of this same basic program exist at the link provided.
* They are named {@link JavaFXAnomalySimpleVersionApplication} and
* {@link JavaFXAnomalyComplexVersionApplication} which is written to throw a
* {@link ConcurrentModificationException} when the JavaFX Application Thread
* becomes reentrant.
*
* I also have a screen grab (not included here) of the stack trace at a
* specific moment <code>handle()/code> is being invoked, and it can clearly
* be seen that the previous executing line was within the scope of execution
* of the previous invocation of <code>handle()</code>.
*
* In the .zip file at the link there is a readme.txt. In that file.
* I present the two lines of code which need to be added, and where
* they need to be added, so as to generate the same stack trace
* showing the same thing.
*/
public class LabelEventHandler implements EventHandler<MouseEvent> {
/**
* a counter which acts as a recursion detector.
* If {@link #handle(MouseEvent)} is never recursively invoked by
* the JavaFX Application Thread, then it's value will never be other
* than 0 (zero) in {@link #showDebugInformation(String)}.
*/
private static int debugCounter;
/**
* The {@link Label} which will disappear when clicked. This causes
* a MOUSE_EXITED_TARGET event top be fired and that in turn causes
* the JavaFX Event Dispatch Thread to recurse into this class's
* {@link #handle(MouseEvent)}
*/
private Label label;
/**
* The {@link Pane} which contains the {@link Label}. The
* {@link Label} is removed from this {@link Pane}.
*/
private final Pane pane;
/**
* Assign the values to the members {@link Pane} and {@link Label}
*/
public LabelEventHandler(Pane pane, Label label) {
this.pane = pane;
this.label = label;
}
/**
* Causes the member {@link #label} to be removed as a child of the
* member {@link #pane}.
*
* @param mouseEvent the {@link MouseEvent} received from the
* JavaFX Application Thread from the {@link Label} which this
* {@link EventHandler} is listening to.
*/
@Override
public void handle(MouseEvent mouseEvent) {
// debug can only every be 0 (zero) at this point
showDebugInformation("ENTERING");
debugCounter++;
if (mouseEvent.getEventType().equals(MouseEvent.MOUSE_PRESSED)
&& mouseEvent.isPrimaryButtonDown()) {
pane.getChildren().remove(label);
}
debugCounter--;
// debug can only every be 0 (zero) at this point
showDebugInformation("EXITING");
}
/**
* Displays two values to standard output. The first is a
* {@link String} indicating whether the
* {@link LabelEventHandler#handle(MouseEvent)} method is
* being entered or exited and the second is the value of
* {@link LabelEventHandler#debugCounter} at the time this
* method is executed.
*
* @param enterOrExit the string ENTERING or EXITING
* reflecting the point at which this method was invoked
* by {@link LabelEventHandler#handle(MouseEvent)}.
*/
private void showDebugInformation(String enterOrExit) {
System.out.println();
System.out.print(enterOrExit + " method handle");
System.out.print(" and debugCounter is " + debugCounter);
System.out.println();
}
}
解决方案
步骤 2 中的处理是否有望在 JavaFX 应用程序线程通过 handle() 将任何进一步事件提交给 X 之前完成?
是的。因此 JavaFX 线程按顺序执行所有操作。例如,如果你要Thread.sleep
在你的handle()
方法中添加一个,那么 JavaFX 线程将不会做任何事情,直到睡眠完成。它按顺序进行所有处理,我猜这是线程的定义元素。它不会关闭并并行处理其他事件。这在动画中非常重要,因为所有这些处理都必须在 JavaFX 线程计算并显示下一帧之前发生。
附录:
考虑一下 - 如果光标在 Rectangle 上,则由于 MouseEvent 删除 Rectangle 会发布一个 MouseEvent.MOUSE_EXIT 事件,因为在 JavaFX 看来,这就是刚刚发生的事情。该 MouseEvent 都是在 JavaFX 应用程序线程上生成的,并将由它处理。现在这是要考虑的事情。JavaFX 应用程序线程可以跟随 MOUSE_EXIT 到 X 的 handle() 或继续 dcoig 任意处理。它是做什么的?
MOUSE_CLICKED 事件将首先得到处理。在线程处理完所有触发事件后,它会绘制到屏幕上。屏幕更新完成后,它将处理任何新触发的事件,例如 MOUSE_EXIT。例如,假设您创建了一个节点,该节点在 MOUSE_ENTERED 上删除该节点,然后将其放回 MOUSE_EXIT。当你将鼠标移到这个节点上时,它会以帧速率闪烁——而不是在更新屏幕之前进入无限循环。
推荐阅读
- powershell - 如何在复制项中使用超时?
- linux - 为什么 mkdir 因 Dockerfile 中的不同基础镜像而失败
- julia - 迭代 Julia 中的数组
- r - R 中的 C3.ai COVID-19 数据湖快速入门
- c++ - c++ : OOP : 如何在单独的头文件中更新数组?
- angular - Angular Material - ({status: 400, message: "validation errors",...} field Errors: [{codes: null, arguments: null ...)
- c# - Nsubstitute 或 moq 一个 Async 方法,因此它将返回错误任务而不是异常
- node.js - 来自客户端的错误数据通过 GraphQL 验证
- jetty - Jetty 在 ktor 上抛出异常,我认为 http2 有问题
- c# - 使用 Azure Rest API 更新测试套件中的自动化测试用例结果