ImageIO write() 是阻塞方法吗? [英] Is ImageIO write() a blocking method?

查看:26
本文介绍了ImageIO write() 是阻塞方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我用 Java 开发的应用程序中,我使用 ImageIO API 来缩放图像,然后将它们写入目录,然后通知 Web 服务器图像已准备好可以使用.在向 Web 服务器发送信号之前的最后一次调用是 ImageIO.write() 方法,但 Web 服务器此时无法找到生成的图像.(Web 服务器能够在几毫秒后看到它们.).

In an application I'm developing in java, I am using the ImageIO API to scale images, then write them to a directory and then inform a web server that the images are ready to be used. The last call before sending a signal to the web server is the method ImageIO.write() but the web server cannot find the generated images at that moment. ( Few milliseconds latter the web server is able to see them. ).

我怀疑 ImageIO.write() 正在异步运行.我搜索了很多,但找不到 ImageIO.write() 是同步操作还是异步操作.如果 ImageIO.write() 没有阻塞程序的流程,是否有一种非黑客的方法让它等到图像写入目标目录?

What I suspect is that ImageIO.write() is running asynchronously. I've searched a lot but i couldn't find if ImageIO.write() is a synchronous or asynchronous operation. If ImageIO.write() is not blocking the flow of the program, is there a non-hacking way to make it wait till the image has been written to the destination directory ?

推荐答案

我无法想象 ImageIO.write 不会阻塞.

I can't imagine that ImageIO.write isn't blocking.

我的假设"是基于 ImageIO.write 可以抛出异常的事实,这表明,即使它在后台写入,它也需要阻塞直到写入完成才能抛出异常.

My "assumption" is based on the fact that ImageIO.write can throws exceptions, which suggests, even if it as writing in the background, it would need to block until the write was finished in order to throw the exceptions.

这可能是缓冲问题?

试着做一个 while (!file.exits()) Thread.sleep(25) 或者有什么停止间隙

Try and do a while (!file.exits()) Thread.sleep(25) or something has a stop gap

这篇关于ImageIO write() 是阻塞方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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