简体   繁体   English

使用JAI从url提取图像时线程被阻塞

[英]Threads getting blocked while fetching image from url using JAI

I am running a service which starts multiple threads processing images using JAI. 我正在运行一个服务,该服务启动使用JAI处理图像的多个线程。 Each thread downloads the image from a url and then applies logic on this image. 每个线程都从URL下载图像,然后在该图像上应用逻辑。 The threads are getting stuck (in waiting) while getting height of the PlanarImage. 在获取PlanarImage的高度时,线程被卡住(等待)。 Any idea why is this happening and how I can avoid it? 知道为什么会发生这种情况以及如何避免吗? Also, the thread which had acquired the lock for the object (on which other threads are waiting) is stuck at socket read. 同样,已获取对象锁的线程(其他线程正在其上等待)被锁定在套接字读取时。 Is there any socket read timeout while reading the images from url? 从url读取图像时是否有任何套接字读取超时?

Code: 码:

public static PlanarImage readImageFromUrl(String url) throws Exception
{
    String urlAddr = URLDecoder.decode(url, Constants.UTF8);
    URL urlS = new URL(urlAddr);

    String operationName = "url";

    ParameterBlockJAI pb = new ParameterBlockJAI(operationName);
    pb.setParameter("URL", urlS);
    pb.setParameter("param", null);

    PlanarImage image = null;
    try
    {
           /** Create a new tilecache object in RenderingHints**/
        image = JAI.create(operationName,pb, (RenderingHints)RenderingHintsFactory.createDefaultRenderingHintsInstance());


        int h = image.getHeight();
        int w = image.getWidth();
        logger.info("Image is "+h +"x"+w+" dim");
        return image;
    }
    catch(Exception e)
    {
        //Release tile cache memory in case of exception
        RenderingHintsFactory.releaseRenderingHints(image);
        throw e;
    }
}

Sample thead stuck at waiting: 示例广告卡在等待中:

"pool-2-thread-97" prio=10 tid=0x0000000050e0b000 nid=0x666d waiting for monitor entry [0x0000000048e74000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.sun.media.jai.codecimpl.JPEGImage.<init>(JPEGImageDecoder.java:105)
- **waiting to lock <0x00000007bca843b8> (a java.lang.Object)**
at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:46)
at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:88)
at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:43)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at     javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:102)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at         javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at com.sun.media.jai.opimage.URLRIF.create(URLRIF.java:74)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at     javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
- locked <0x00000007f6a5e3a0> (a javax.media.jai.RenderedOp)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
- locked <0x00000007f6a5e3a0> (a javax.media.jai.RenderedOp)
at javax.media.jai.RenderedOp.getHeight(RenderedOp.java:2188)
at com.amazon.lmclassifier.daemon.image.util.ImageUtils.readImageFromUrl(ImageUtils.java:66)

THREAD WHICH HAS LOCKED THE OBJECT: 锁定对象的线程:

"pool-2-thread-96" prio=10 tid=0x0000000050e08800 nid=0x666c runnable [0x0000000048d72000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
- locked <0x00000007f6af46f0> (a java.io.BufferedInputStream)
at sun.net.www.MeteredStream.read(MeteredStream.java:116)
- locked <0x00000007f6af4718> (a sun.net.www.http.KeepAliveStream)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2668)
at com.sun.media.jai.codec.FileCacheSeekableStream.readUntil(FileCacheSeekableStream.java:125)
at com.sun.media.jai.codec.FileCacheSeekableStream.read(FileCacheSeekableStream.java:258)
at com.sun.media.jai.codec.ForwardSeekableStream.read(ForwardSeekableStream.java:54)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)
- locked <0x00000007f6af47e8> (a sun.awt.image.codec.JPEGImageDecoderImpl)
at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:210)
- locked <0x00000007f6af47e8> (a sun.awt.image.codec.JPEGImageDecoderImpl)
at com.sun.media.jai.codecimpl.JPEGImage.<init>(JPEGImageDecoder.java:110)
**- locked <0x00000007bca843b8> (a java.lang.Object)**
at com.sun.media.jai.codecimpl.JPEGImageDecoder.decodeAsRenderedImage(JPEGImageDecoder.java:46)
at com.sun.media.jai.opimage.CodecRIFUtil.create(CodecRIFUtil.java:88)
at com.sun.media.jai.opimage.JPEGRIF.create(JPEGRIF.java:43)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:102)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at com.sun.media.jai.opimage.URLRIF.create(URLRIF.java:74)
at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.media.jai.FactoryCache.invoke(FactoryCache.java:122)
at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1674)
at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:473)
at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:332)
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:819)
- locked <0x00000007f6af49e0> (a javax.media.jai.RenderedOp)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:867)
- locked <0x00000007f6af49e0> (a javax.media.jai.RenderedOp)
at javax.media.jai.RenderedOp.getHeight(RenderedOp.java:2188)
at com.amazon.lmclassifier.daemon.image.util.ImageUtils.readImageFromUrl(ImageUtils.java:66)
‹ ImageReader thread safety Threads getting blocked while fetching image from url › 

I figured out from some other post that the JPEGImageDecoder.java uses a mutex object to syncronize image reads. 我从其他文章中发现JPEGImageDecoder.java使用互斥对象同步图像读取。 And hence the threads are getting blocked. 因此线程被阻塞了。 http://www.java.net/node/677181 Any idea on how can I resolve this? http://www.java.net/node/677181关于如何解决此问题的任何想法?

In some cases for images loading occurs lazily in the background, until getHeight() or getWidth() is called, which then wait for the loading to be finished. 在某些情况下,图像的加载会在后台延迟进行,直到调用getHeight()getWidth() ,然后等待加载完成。 One just has to postpone using the image to a point in the future. 只需将使用该图像的时间推迟到将来。

To have less problems, you might first load the files from the net, and then load them as images from your local file system. 为了减少问题,您可以首先从网上加载文件,然后从本地文件系统将它们作为图像加载。 This would allow indicating the progress too. 这也可以指示进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM