可以安全地在单独的线程中更新BufferedImage的单独区域吗? [英] Safe to update separate regions of a BufferedImage in separate threads?

查看:127
本文介绍了可以安全地在单独的线程中更新BufferedImage的单独区域吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BufferedImage 实例的集合,一个主图像和一些主图像,它们是通过在main上调用 getSubImage 创建的图片。子图像不重叠。我也在修改子图像,我想把它分成多个线程,每个子图像一个。

I have a collection of BufferedImage instances, one main image and some subimages created by calling getSubImage on the main image. The subimages do not overlap. I am also making modifications to the subimage and I want to split this into multiple threads, one per subimage.

从我对 BufferedImage <的理解/ code>, Raster DataBuffer 工作,这应该是安全的,因为:

From my understanding of how BufferedImage, Raster and DataBuffer work, this should be safe because:


  • 每个 BufferedImage 的实例(及其各自的 WritableRaster SampleModel )仅从一个线程访问。

  • 共享 ColorModel 是不可变的

  • DataBuffer 没有可以修改的字段(唯一可以改变的是支持数组的元素。 )

  • 在单独的线程中修改数组的不相交段是安全的。

  • Each instance of BufferedImage (and its respective WritableRaster and SampleModel) is accessed from only one thread.
  • The shared ColorModel is immutable
  • The DataBuffer has no fields that can be modified (the only thing that can change is elements of the backing array.)
  • Modifying disjoint segments of an array in separate threads is safe.

但是我不能在文档中找到任何说明这样做绝对安全的内容。我可以认为它是安全的吗?我知道有可能处理孩子 Raster 的副本,但由于内存限制,我宁愿避免这种情况。

However I cannot find anything in the documentation that says that it is definitely safe to do this. Can I assume it is safe? I know that it is possible to work on copies of the child Rasters but I would prefer to avoid this because of memory constraints.

否则,是否可以在不复制父图像区域的情况下使操作成为线程安全的?

Otherwise, is it possible to make the operation thread-safe without copying regions of the parent image?

推荐答案

你有没有考虑使用JAI来管理你的'子图像'作为瓷砖?如果你不必挂起原始图像BufferedImage实例以及它的所有subImage BufferedImage实例,它似乎更好地利用了资源。有关JAI的信息可以在这里找到:
JAI README

Have you looked into using JAI to manage your 'subimages' as tiles? It seems like a better use of resources if you don't have to hang onto the original image BufferedImage instance as well as all its subImage BufferedImage instance. Information about JAI can be found here: JAI README

有一个类TiledImage,它实现了RenderedImage接口(给它一个带BufferedImage的共同祖先)。根据JAI文档:

There is a class, TiledImage, which implements the RenderedImage interface (giving it a common ancestor with BufferedImage). According to the JAI documentation:


使用平铺还有助于
使用多个线程进行
计算。之前分配的
磁贴也可以重新用于保存
内存。

The use of tiling also facilitates the use of multiple threads for computation. Previously allocated tiles may also be re-used to save memory.

使用其中一个实现无论如何,RenderedImage通常更喜欢BufferedImage,因为BufferedImage在内存中为整个图像维护一个图像快照。 JAI使用渲染链,可以根据需要回收切片以适应内存约束。

Using one of these implementations of RenderedImage is often preferrable to a BufferedImage anyway, since the BufferedImage maintains an image snapshot in memory for the entire image. JAI uses a rendering chain and can recycle tiles as needed to fit memory constraints.

这篇关于可以安全地在单独的线程中更新BufferedImage的单独区域吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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