ImageIO线程安全 [英] ImageIO thread-safety

查看:506
本文介绍了ImageIO线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 javax.imageio 规范中看到线程安全不是目标,尽管我已经看过几个使用 ImageIO.read的例子() ImageIO.write()用于在网络环境中上传/清理图像。

I see in the javax.imageio spec that thread-safety is not a goal, despite I've seen several examples using ImageIO.read() and ImageIO.write() for uploading/sanitizing images in a web environment.

所以,我的问题是,尽管规范说的是 ImageIO 线程安全?

So, my question is, despite what the spec says, is ImageIO thread-safe?

推荐答案

TLDR;是的,静态方法 ImageIO.read(...) write(...)是线程安全的。

TLDR; Yes, the static methods ImageIO.read(...) and write(...) are thread safe.

规范中的部分 线程安全不是目标 需要在上下文中阅读。实际上说的是个人 ImageReader ImageWriter ImageInputStream / ImageOutputStream 实现不需要关心线程安全(因此,客户端代码永远不应该假设它们是线程安全的)。只要你遵守这条规则,你就是安全的。但是请注意,规范的相同部分还指出:

The part of the specification that says "thread safety is not a goal" needs to be read in context. What the spec actually says, is that individual ImageReader, ImageWriter or ImageInputStream/ImageOutputStream implementations do not need to be concerned with thread safety (and as a result, client code should never assume they are thread safe). As long as you live by this rule, you are safe. Note however, that the same part of the spec also states that:


[...]它必须可以用于多个实例同一个插件类同时运行。

[...] it must be possible for multiple instances of the same plug-in class to operate simultaneously.

本部分规范不讨论的静态方法ImageIO 特别是,但上面的引用暗示tese方法是线程安全的,因为 ImageIO.read(...) write(...)创建 ImageInputStream的新实例 / ImageOutputStream 和<$每次调用c $ c> ImageReader / ImageWriter 。所以,它不是真的尽管规范说

This part of the specification does not discuss the static methods of ImageIO especially, but the above quote implies that tese methods are thread safe, as ImageIO.read(...) and write(...) creates new instances of ImageInputStream/ImageOutputStream and ImageReader/ImageWriter for each invocation. So, it's not really "despite what the spec says".

ImageIO class由几个可以安全使用的静态方法组成,类本身(大多数是*)是无状态的。如果它不能以这种方式工作,它就没有多大用处...

The ImageIO class consists of several other static methods that are safe to use, and the class itself is (mostly*) stateless. If it didn't work this way, it wouldn't really be of much use...

*) IIORegistry 实例 ImageIO 在类创建时填充,并在 scanForPlugins()方法时重新初始化调用。如果两个线程同时调用它,您可能会遇到问题(即插件可能无法正确注册),但是当客户端代码控制它发生的地方/时间时,您可以轻松避免这种情况。还有每个线程组 CacheInfo ,但它的用法似乎正确同步。

*) The IIORegistry instance of ImageIO is populated at class creation time, and re-initialized whenever the scanForPlugins() method is invoked. You might run into problems (i.e. plugins might not be correctly registered) if two threads invoke it the same time, but as client code control where/when it happens, you can easily avoid that. There's also the per thread group CacheInfo, but its usage seems to be properly synchronized.

免责声明,我没有编写规范,但这是我的解释(我在无数的多线程应用程序中使用了 ImageIO ,以及编写十几个 ImageReader ImageWriter 自己插件。)

Disclaimer, I didn't write the spec, but this is my interpretation (and I have used ImageIO in countless, multithreaded applications, as well as written a dozen or so ImageReader and ImageWriter plugins myself).

这篇关于ImageIO线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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