ImageIO - 读取不同的文件并发问题?

时间:2018-05-22 06:38:22

标签: java multithreading kotlin javax.imageio

我目前正在编写一个程序,批处理不同的图像。所以我认为并行操作(缩放/添加水印)可能很聪明。

问题是我收到以下错误:

    Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1006)
    at eu.reisihub.soft.watermarking.Main$main$2$2$2$2.invoke(Main.kt:62)
    at eu.reisihub.soft.watermarking.Main$main$2$2$2$2.invoke(Main.kt:19)
    at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:149)
    at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:149)
    at kotlin.sequences.SequencesKt___SequencesKt.count(_Sequences.kt:1006)
    at eu.reisihub.soft.watermarking.Main.main(Main.kt:66)
Caused by: java.lang.NullPointerException
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
    at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:603)
    ... 7 more
Caused by: java.lang.NullPointerException
    at java.desktop/java.awt.color.ICC_Profile.intFromBigEndian(ICC_Profile.java:1784)
    at java.desktop/java.awt.color.ICC_Profile.getNumComponents(ICC_Profile.java:1476)
    at java.desktop/sun.java2d.cmm.lcms.LCMSTransform.<init>(LCMSTransform.java:93)
    at java.desktop/sun.java2d.cmm.lcms.LCMS.createTransform(LCMS.java:173)
    at java.desktop/java.awt.color.ICC_ColorSpace.fromRGB(ICC_ColorSpace.java:230)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.setImageData(JPEGImageReader.java:808)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:723)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:347)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:493)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:716)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1173)
    at java.desktop/com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1153)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1468)
    at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363)
    at eu.reisihub.shot.UtilsKt.readImage(Utils.kt:19)
    at eu.reisihub.soft.watermarking.WatermarkUtils$create$1.invoke(WatermarkUtils.kt:18)
    at eu.reisihub.soft.watermarking.WatermarkUtils$create$1.invoke(WatermarkUtils.kt:8)
    at eu.reisihub.soft.watermarking.Main$sam$java_util_concurrent_Callable$0.call(Main.kt)
    at java.base/java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1448)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1603)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)

请注意,我知道java.desktop/java.awt.color.ICC_Profile.intFromBigEndian(ICC_Profile.java:1784)中有一个NPE。相信我,这不是形象。

我尝试了Java 8和Java 10的两种方法。我有一个固定大小的线程池,有6个线程,用于4个核心。我正在使用Kotlin进行实现,并创建了一个扩展函数fun Path.readImage(): BufferedImage。对于那些不了解Kotlin的人,仅出于本示例的目的,大致相当于public BufferedImage readImage(Path this)

天真的方法 使用Files.newInputStream(this, StandardOpenOption.READ).use { ImageIO.read(it) }读取文件。这将从Path打开一个新的InputStream,并告诉ImageIO从此InputStream中读取图像。看起来不错?在Java 8上每隔四次运行一次,在Java 10上运行大约90%.BTW:如果我在这周围包装一个synchronized块,它总能正常工作。用13秒钟处理17张图像而不是7秒钟。

与此同时,我将Gradle从4.4更新到4.7。在我将IntelliJ配置为使用Java 10和Gradle来使用Java 8之前。可能是上面突出显示的差异的原因。边注。我总是使用synchronized(System.err)

复杂的方法 知道它在同步所有内容时工作现在我的方法看起来更复杂,如下所示:

fun Path.readImage(): BufferedImage = Files.newInputStream(this, StandardOpenOption.READ).buffered().use {
    var nStream: ImageInputStream? = null
    var nReader: ImageReader? = null
    synchronized(System.err) {
        nStream = ImageIO.createImageInputStream(it) ?: throw IIOException("Can't create an ImageInputStream!")

        val iter = ImageIO.getImageReaders(nStream)
        if (!iter.hasNext()) {
            throw IIOException("No image nReader found!")
        }
        nReader = iter.next()
    }

    nStream!!.let { stream ->
        nReader!!.let { reader ->
            reader.setInput(stream, true, true)
            try {
                println(reader)
                return reader.read(0, null)
            } finally {
                reader.dispose()
                stream.close()
            }
        }
    }
}

请参阅代码中的println?我知道,对于每个Image,都会创建一个新的com.sun.imageio.plugins.jpeg.JPEGImageReader对象。这样可以将性能提高到我说话的7秒钟。通常不工作2-4次然后工作6-10次。

复杂版本的堆栈跟踪如下:

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.NullPointerException
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:192)
    at eu.reisihub.soft.watermarking.Main$main$1$2$2$2$2.invoke(Main.kt:64)
    at eu.reisihub.soft.watermarking.Main$main$1$2$2$2$2.invoke(Main.kt:20)
    at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:149)
    at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:149)
    at kotlin.sequences.SequencesKt___SequencesKt.count(_Sequences.kt:1006)
    at eu.reisihub.soft.watermarking.Main$main$1.invoke(Main.kt:68)
    at eu.reisihub.soft.watermarking.Main$main$1.invoke(Main.kt:20)
    at eu.reisihub.shot.UtilsKt.measured(Utils.kt:54)
    at eu.reisihub.soft.watermarking.Main.main(Main.kt:24)
Caused by: java.lang.NullPointerException
    at java.awt.color.ICC_Profile.intFromBigEndian(ICC_Profile.java:1782)
    at java.awt.color.ICC_Profile.getNumComponents(ICC_Profile.java:1474)
    at sun.java2d.cmm.lcms.LCMSTransform.<init>(LCMSTransform.java:98)
    at sun.java2d.cmm.lcms.LCMS.createTransform(LCMS.java:173)
    at java.awt.color.ICC_ColorSpace.fromRGB(ICC_ColorSpace.java:218)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.setImageData(JPEGImageReader.java:694)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(JPEGImageReader.java:609)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(JPEGImageReader.java:347)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(JPEGImageReader.java:481)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(JPEGImageReader.java:602)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1059)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039)
    at eu.reisihub.shot.UtilsKt.readImage(Utils.kt:39)
    at eu.reisihub.soft.watermarking.WatermarkUtils$create$1.invoke(WatermarkUtils.kt:16)
    at eu.reisihub.soft.watermarking.WatermarkUtils$create$1.invoke(WatermarkUtils.kt:8)
    at eu.reisihub.soft.watermarking.Main$sam$java_util_concurrent_Callable$0.call(Main.kt)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)

Main:64 Future#get的位置 Main:68kotlin.Sequence#count

Callable<Task>

定位Main.kt文件:https://github.com/reisi007/Reisishot-Photo-Tools/blob/ea32a605657226edb6140911fd02f77d006163d3/Watermarking/src/main/kotlin/eu/reisihub/soft/watermarking/Main.kt

Utils.kt文件的位置:https://github.com/reisi007/Reisishot-Photo-Tools/blob/ea32a605657226edb6140911fd02f77d006163d3/base/src/main/kotlin/eu/reisihub/shot/Utils.kt

我已经尝试过几个小时来改进代码,现在我的脑袋已停止产生新的想法了。我知道错误也发生在2张照片上。由于它并不总是失败,因此ICC配置文件必须正确。老实说 - 我知道我的硬盘驱动器没有声音,偶尔会搞砸。我也试过从我的SSD读取文件。根据{{​​3}},read方法应该是线程安全的。我不知道发生了什么事。磁盘上的每个映像(由路径表示)都会获得它自己的at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039) at eu.reisihub.shot.UtilsKt.readImage(Utils.kt:39) 。我只是阅读这些图像并将其写入不同的文件夹。所以foreach任务一个文件被读取,一个文件被写入。这两个文件不一样,每个任务都有自己的文件。

他们唯一共享的是PNG格式的BufferedImage,它是在读取的图像(水印)上绘制的。但这发生在以后。可以在堆栈跟踪中看到它在读取JPEG图像时发生。

rlang

如果要执行该程序,可以使用https://stackoverflow.com/a/26300361/1870799

创建主应用程序所需路径的JSON设置

我感谢任何意见。目前我不知道出了什么问题......

BTW:对于我正在使用的一些任务:https://github.com/reisi007/Reisishot-Photo-Tools/blob/ea32a605657226edb6140911fd02f77d006163d3/Watermarking/src/main/kotlin/eu/reisihub/soft/watermarking/SettingsCreator.kt

2 个答案:

答案 0 :(得分:2)

在多线程应用程序中,OpenJDK有bug report标题为'来自ICC_Profile.getInstance()的NullPointerException'。在撰写本文时尚无决议。

答案 1 :(得分:2)

更新2018-05-23

@haraldK在评论中提到加载图像配置文件之前 代码:

import java.awt.color.ColorSpace
import java.awt.color.ICC_Profile
import java.awt.image.BufferedImage
import java.nio.file.Path
import javax.imageio.ImageIO

object ImgLoadUtils {
    init {
        // Load deferred color space profiles to avoid 
        // ConcurrentModificationException due to JDK
        // Use in public static main void or prior to application initialization
        // https://github.com/haraldk/TwelveMonkeys/issues/402
        // https://bugs.openjdk.java.net/browse/JDK-6986863
        // https://stackoverflow.com/questions/26297491/imageio-thread-safety
        ICC_Profile.getInstance(ColorSpace.CS_sRGB).getData();
        ICC_Profile.getInstance(ColorSpace.CS_PYCC).getData();
        ICC_Profile.getInstance(ColorSpace.CS_GRAY).getData();
        ICC_Profile.getInstance(ColorSpace.CS_CIEXYZ).getData();
        ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB).getData();
    }

    fun loadImage(p: Path): BufferedImage = ImageIO.read(p.toFile())
}

SOURCE

感谢@Alex Taylor提供的OpenJDK错误链接。这让我确信我不是一个白痴。

而不是

fun Path.readImage(): BufferedImage =
    Files.newInputStream(this, StandardOpenOption.READ).use { ImageIO.read(it) }

我现在正在使用

fun Path.readImage(): BufferedImage =
    ImageIcon(toUri().toURL()).let {
        BufferedImage(it.iconWidth, it.iconHeight, BufferedImage.TYPE_INT_ARGB).apply {
            it.paintIcon(null, createGraphics(), 0, 0)
        }
    }

这是https://aacsinia.wordpress.com/2010/12/21/java-how-to-create-buffered-image-from-inputstream/。速度与标准差不多,错误消失[ 20+次]!因此,恕我直言ImageIO.read不应在多线程环境中使用....