ObjectInputStream 死鎖問題

  1. ObjectInputStream與ObjectOutputStream的順序問題
在網絡通訊中,主機與客戶端若使用ObjectInputStream與ObjectOutputStream建立對象通訊,必須注重聲明此兩個對象的順序。
如:
主機端先建立ObjectInputStream後建立ObjectOutputStream,則對應地客戶端要先建立ObjectOutputStream後建立ObjectInputStream,否則會造成兩方互相等待數據而導致死鎖。
原因是建立ObjectInputStream對象是需要先接收一定的header數據,接收到這些數據之前會處於阻塞狀態。以下爲java API文檔的說明
Creates an ObjectInputStream that reads from the specified InputStream.
A serialization stream header is read from the stream and verified.
This constrUCtor will block until the corresponding ObjectOutputStream
has written and flushed the header.

 

 

http://tech.cncms.com/web/java/32345.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章