Java序列化,ObjectInputStream.readObject(),检查是否会阻塞 [英] Java serialization, ObjectInputStream.readObject(), check if will block
问题描述
我正在使用 ObjectInputStream
来调用 readObject
来读取序列化 Objects
。我想避免使用此方法块,所以我希望使用类似 Inputstream.available()
的内容。
I'm using an ObjectInputStream
to call readObject
for reading in serialized Objects
. I would like to avoid having this method block, so I'm looking to use something like Inputstream.available()
.
InputStream.available()
会告诉你有可用的字节,并且 read()
不会阻止。是否存在一种等效的串行化方法,它会告诉您是否有对象
可用且 readObject
是否会阻塞?
InputStream.available()
will tell you there are bytes available and that read()
will not block. Is there an equivalent method for seriailzation that will tell you if there are Object
s available and readObject
will not block?
推荐答案
不。虽然您可以在另一个线程中使用 ObjectInputStream
并检查是否有可用的对象。一般来说,轮询并不是一个好主意,特别是对于 InputStream.available
的保证不佳。
No. Although you could use the ObjectInputStream
in another thread and check to see whether that has an object available. Generally polling isn't a great idea, particularly with the poor guarantees of InputStream.available
.
这篇关于Java序列化,ObjectInputStream.readObject(),检查是否会阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!