在 JavaFX 应用程序线程以外的线程上调用 ImageIO 的 JavaFX 应用程序挂起 Mac OS X

JavaFX application with an ImageIO call on a thread other than JavaFX Application Thread hangs on Mac OS X

假设我有一个示例 JavaFX 应用程序,它使用从应用程序的 JAR 中读取的图像更新其 UI,并以延迟的方式执行此操作(即图像 显示 UI 之后绘制:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

public final class SameThreadAsync extends Application {
    @Override
    public void start(final Stage primaryStage) {
        final ImageView imageView = new ImageView();
        imageView.setPreserveRatio(true);
        imageView.setSmooth(true);
        imageView.setFitWidth(300.0);
        imageView.setFitHeight(300.0);

        Platform.runLater(() -> {
            final BufferedImage image = getIcon();
            imageView.setImage(SwingFXUtils.toFXImage(image, null));
        });

        final Node label = new Label(null, imageView);

        final StackPane root = new StackPane();
        root.getChildren().add(label);
        primaryStage.setScene(new Scene(root, 300.0, 300.0));
        primaryStage.show();
    }

    private BufferedImage getIcon() {
        System.out.println("Reading an image from thread " + Thread.currentThread().getName());

        try (final InputStream in = new BufferedInputStream(getClass().getResourceAsStream("logo1.png"))) {
            return ImageIO.read(in);
        } catch (final IOException ioe) {
            ioe.printStackTrace();
            return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
        }
    }

    public static void main(final String ... args) {
        launch(args);
    }
}

以上代码运行良好。现在,考虑到我想在一个单独的线程中加载图像并在 JavaFX Application Thread 上处理结果,所以我将重写代码如下:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class SeparateThreadAsync extends Application {
    private static final ExecutorService IO_EXECUTOR = Executors.newSingleThreadExecutor(r -> new Thread(r, "I/O Queue"));

    @Override
    public void start(final Stage primaryStage) {
        final ImageView imageView = new ImageView();
        imageView.setPreserveRatio(true);
        imageView.setSmooth(true);
        imageView.setFitWidth(300.0);
        imageView.setFitHeight(300.0);

        IO_EXECUTOR.submit(() -> {
            final BufferedImage image = getIcon();
            Platform.runLater(() -> imageView.setImage(SwingFXUtils.toFXImage(image, null)));
        });

        final Node label = new Label(null, imageView);

        final StackPane root = new StackPane();
        root.getChildren().add(label);
        primaryStage.setScene(new Scene(root, 300.0, 300.0));
        primaryStage.show();
    }

    private BufferedImage getIcon() {
        System.out.println("Reading an image from thread " + Thread.currentThread().getName());

        try (final InputStream in = new BufferedInputStream(getClass().getResourceAsStream("logo1.png"))) {
            return ImageIO.read(in);
        } catch (final IOException ioe) {
            ioe.printStackTrace();
            return new BufferedImage(0, 0, BufferedImage.TYPE_INT_ARGB);
        }
    }

    public static void main(final String ... args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            IO_EXECUTOR.shutdown();
            System.out.println("I/O Queue shut down.");
        }));

        launch(args);
    }
}

重写的代码在 LinuxWindows 上都能正常工作,但在 [=67= 上挂起] OS X 10.14.5 (Mojave), Oracle JDK 1.8.0_192 还有 JetBrains Runtime 1.8.0_202.

部分线程转储为:

"I/O Queue" #19 prio=5 os_prio=31 tid=0x00007ffe3d85a000 nid=0x12007 runnable [0x0000700004822000]
   java.lang.Thread.State: RUNNABLE
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
    - locked <0x000000076ab068a8> (a java.util.Vector)
    - locked <0x000000076ab06900> (a java.util.Vector)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
    at java.lang.Runtime.load0(Runtime.java:809)
    - locked <0x000000076ab1dea8> (a java.lang.Runtime)
    at java.lang.System.load(System.java:1086)
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
    - locked <0x000000076ab068a8> (a java.util.Vector)
    - locked <0x000000076ab06900> (a java.util.Vector)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1845)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    - locked <0x000000076ab1dea8> (a java.lang.Runtime)
    at java.lang.System.loadLibrary(System.java:1122)
    at java.awt.Toolkit.run(Toolkit.java:1636)
    at java.awt.Toolkit.run(Toolkit.java:1634)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.awt.Toolkit.loadLibraries(Toolkit.java:1633)
    at java.awt.Toolkit.<clinit>(Toolkit.java:1670)
    at sun.awt.AppContext.run(AppContext.java:277)
    at sun.awt.AppContext.run(AppContext.java:266)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.AppContext.initMainAppContext(AppContext.java:266)
    at sun.awt.AppContext.access0(AppContext.java:135)
    at sun.awt.AppContext.run(AppContext.java:321)
    - locked <0x000000076c238c00> (a sun.awt.AppContext$GetAppContextLock)
    at sun.awt.AppContext.run(AppContext.java:304)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.AppContext.getAppContext(AppContext.java:303)
    at javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:154)
    at javax.imageio.ImageIO.<clinit>(ImageIO.java:66)
    at com.example.SeparateThreadAsync.getIcon(SeparateThreadAsync.java:49)
    at com.example.SeparateThreadAsync.lambda$start(SeparateThreadAsync.java:33)
    at com.example.SeparateThreadAsync$$Lambda/1820086024.run(Unknown Source)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

"JavaFX Application Thread" #15 prio=5 os_prio=31 tid=0x00007ffe3b0e4000 nid=0x307 waiting for monitor entry [0x00007ffee75f1000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at java.lang.Runtime.load0(Runtime.java:801)
    - waiting to lock <0x000000076ab1dea8> (a java.lang.Runtime)
    at java.lang.System.load(System.java:1086)
    at com.sun.glass.utils.NativeLibLoader.loadLibraryFullPath(NativeLibLoader.java:201)
    at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:94)
    at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:39)
    - locked <0x000000076b4185c0> (a java.lang.Class for com.sun.glass.utils.NativeLibLoader)
    at com.sun.javafx.font.PrismFontFactory.lambda$static4(PrismFontFactory.java:100)
    at com.sun.javafx.font.PrismFontFactory$$Lambda/1988937384.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.font.PrismFontFactory.<clinit>(PrismFontFactory.java:98)
    at com.sun.javafx.text.PrismTextLayout.<clinit>(PrismTextLayout.java:67)
    at com.sun.javafx.text.PrismTextLayoutFactory.<clinit>(PrismTextLayoutFactory.java:33)
    at com.sun.javafx.tk.quantum.QuantumToolkit.getTextLayoutFactory(QuantumToolkit.java:1086)
    at com.sun.javafx.scene.control.skin.Utils.<clinit>(Utils.java:90)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.sun.javafx.css.StyleManager.getURL(StyleManager.java:863)
    at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1075)
    - locked <0x000000076c3bc948> (a java.lang.Object)
    at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:935)
    at com.sun.javafx.css.StyleManager._setDefaultUserAgentStylesheet(StyleManager.java:1395)
    - locked <0x000000076c3bc948> (a java.lang.Object)
    at com.sun.javafx.css.StyleManager.setUserAgentStylesheets(StyleManager.java:1227)
    - locked <0x000000076c3bc948> (a java.lang.Object)
    at com.sun.javafx.application.PlatformImpl.lambda$_setPlatformUserAgentStylesheet1(PlatformImpl.java:698)
    at com.sun.javafx.application.PlatformImpl$$Lambda/566730701.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl._setPlatformUserAgentStylesheet(PlatformImpl.java:697)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:548)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
    at com.example.SeparateThreadAsync.start(SeparateThreadAsync.java:37)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda/1315653396.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1212899836.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1963951195.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda/1289696681.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

所以这似乎是某种竞争条件。有趣的是,类似的 JFC/Swing 应用程序根本没有挂起。

我在 Kotlin 中使用协程时第一次遇到这种行为。例如,this code doesn't hang while this one 会。

问题:

  1. 任何人都可以解释上述行为吗?有没有可能我遗漏了一些东西而不是在 JDK?
  2. 中看到错误
  3. 如何进一步诊断问题?

上述行为似乎是 Mac OS X Oracle JDK[ 实现中的错误=17=] -- 这个问题只出现在 Java 1.8.0,我无法在 Java 9.0 上重现它.410.0.2.

我正在编写一个需要在单独的线程上延迟加载图像缩略图的应用程序。 ImageIO.read() 也挂了。但是有人建议做

new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);

在启动其他线程之前在主应用程序线程中,因为这显然会导致 AWT 第一次 运行 一些必须在主线程中完成的初始化代码。这解决了 ImageIO.read 问题。

但是调用的时候也是挂了

SwingFXUtils.toFXImage(bufferedImage, null);

所以我将它移到我的 "Platform.runLater(...)" 调用中,这样它就可以在 JavaFX 线程上完成,并且它起作用了,尽管它并不理想,因为我希望这种转换发生在单独的线程,因此 UI 将更具响应性。但是这种转换发生在内存中,所以我可以接受它。

@user3763100 的解决方案对我不起作用,相反我做了:

if (SystemUtils.IS_OS_MAC_OSX) {
    JFrame frame = new JFrame();
}

在应用程序初始化方法中。