在Mac OS X上挂起在JavaFX Application Thread以外的线程上执行ImageIO调用的JavaFX应用程序 [英] JavaFX application with an ImageIO call on a thread other than JavaFX Application Thread hangs on Mac OS X

查看:97
本文介绍了在Mac OS X上挂起在JavaFX Application Thread以外的线程上执行ImageIO调用的JavaFX应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下,我有一个示例 JavaFX 应用程序,该应用程序使用从应用程序的JAR中读取的图像更新其UI,并以延迟的方式进行操作(即,在之后绘制了该图像>显示用户界面):

Consider I have a sample JavaFX application which updates its UI with an image read from the application's JAR, and does so in a delayed manner (i.e. the image is painted after the UI is shown):

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上处理结果,所以我将代码重写如下:

The above code works fine. Now, consider I want to load the image in a separate thread and process the result on the JavaFX Application Thread, so I'll rewrite the code as follows:

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);
    }
}

重新编写的代码在 Linux Windows 上都可以正常运行,但在Mac OS X 10.14.5 ( Mojave ),Oracle JDK 1.8.0_192 以及 JetBrains运行时 1.8.0_202 .

The rewritten code works fine on both Linux and Windows but hangs on Mac OS X 10.14.5 (Mojave), Oracle JDK 1.8.0_192 and also 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$3.run(Toolkit.java:1636)
    at java.awt.Toolkit$3.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$2.run(AppContext.java:277)
    at sun.awt.AppContext$2.run(AppContext.java:266)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.awt.AppContext.initMainAppContext(AppContext.java:266)
    at sun.awt.AppContext.access$400(AppContext.java:135)
    at sun.awt.AppContext$3.run(AppContext.java:321)
    - locked <0x000000076c238c00> (a sun.awt.AppContext$GetAppContextLock)
    at sun.awt.AppContext$3.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$2(SeparateThreadAsync.java:33)
    at com.example.SeparateThreadAsync$$Lambda$61/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$static$244(PrismFontFactory.java:100)
    at com.sun.javafx.font.PrismFontFactory$$Lambda$70/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$_setPlatformUserAgentStylesheet$181(PlatformImpl.java:698)
    at com.sun.javafx.application.PlatformImpl$$Lambda$65/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$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$56/1315653396.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1212899836.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1963951195.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1289696681.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

因此,这似乎是某种竞争状况.有趣的是,一个类似的 JFC/Swing 应用程序根本不会挂起.

so it seems like some sort of a race condition. Interestingly, a similar JFC/Swing application doesn't hang at all.

我在与 Kotlin 一起使用协程时第一次遇到这种行为.例如,此代码不会在

I first encountered this behaviour while playing with coroutines in Kotlin. For example, this code doesn't hang while this one does.

问题:

  1. 有人可以解释上述行为吗?我有什么机会会丢失某些东西,而不是看到JDK中的错误吗?
  2. 如何进一步诊断问题?

推荐答案

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

I'm writing an application that needs to lazy-load image thumbnails on a separate thread. ImageIO.read() was hanging, too. But someone suggested doing

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

在启动其他线程之前,先在主应用程序线程中使用

,因为这显然会导致AWT第一次运行一些初始化代码,而这必须在主线程中完成.这就解决了ImageIO.read问题.

in the main application thread before starting other threads, as this apparently causes AWT to run some initialization code the first time which has to be done in the main thread. That solved the ImageIO.read problem.

但在致电

SwingFXUtils.toFXImage(bufferedImage, null);

所以我将其移到了"Platform.runLater(...)"调用中,以便可以在JavaFX线程上完成,尽管不理想,但仍可行,因为我希望进行这种转换在单独的线程上,因此UI响应速度更快.但是这种转换发生在内存中,所以我可以忍受它.

so I moved that inside my "Platform.runLater(...)" call so that it would be done on the JavaFX thread, and that worked, though it isn't ideal, since I would like that conversion to happen on the separate thread so the UI will be more responsive. But that conversion happens in memory, so I can live with it.

这篇关于在Mac OS X上挂起在JavaFX Application Thread以外的线程上执行ImageIO调用的JavaFX应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆