Re: [問題] Tomcat雜問

看板java作者 (Zzz...)時間12年前 (2012/05/06 15:14), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串3/3 (看更多)
※ 引述《LaPass (LaPass)》之銘言: : → LaPass:現在我看到Serializable就頭大.... 這東西不好用....orz 05/05 15:15 : → LaPass:4板... 有點久不過我先去找來看看 05/05 15:27 : 推 PsMonkey:敢問 Serializable 哪裡不好用? 05/06 01:04 : 看到libary中有 Serializable 這個東西 : 通常就是準備一個物件存成檔案、透過網路線傳到別的地方去之類的 : 大部分的狀況下,用起來還不錯 : 但是我在傳的時候常常遇到一種問題 [deleted] : 雖然我沒看到解釋這種狀況的文 : 但是我猜Serializable在傳遞Object時,會先檢查參照 : 如果有跟曾經傳過的物件的參照一樣,就直接使用上一次傳過去的值 : 所以,在用Serializable的時候,不小心的話會出現難以發現的bug 這現像在 2000 年的這文章裡有提到,以下節錄可供參考的部分 (整篇文章也值得一讀) http://java.sun.com/developer/technicalArticles/Programming/serialization/ Caching Objects in the Stream First, consider the situation in which an object is written to a stream and then written again later. By default, an ObjectOutputStream will maintain a reference to an object written to it. That means that if the state of the written object is written and then written again, the new state will not be saved! Here is a code snippet that shows that problem in action: 10 ObjectOutputStream out = new ObjectOutputStream(...); 20 MyObject obj = new MyObject(); // must be Serializable 30 obj.setState(100); 40 out.writeObject(obj); // saves object with state = 100 50 obj.setState(200); 60 out.writeObject(obj); // does not save new object state There are two ways to control that situation. First, you could make sure to always close the stream after a write call, ensuring the new object is written out each time. Second, you could call the ObjectOutputStream.reset() method, which would tell the stream to release the cache of references it is holding so all new write calls will actually be written. Be careful, though -- the reset flushes the entire object cache, so all objects that have been written could be rewritten. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 98.26.14.35

05/06 15:41, , 1F
謝謝,當初找資料時也看過reset這個東西
05/06 15:41, 1F

05/06 17:11, , 2F
reset 勤一點也比較不容易 OutOfMemory
05/06 17:11, 2F
文章代碼(AID): #1FfYJp3n (java)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
問題
5
16
文章代碼(AID): #1FfYJp3n (java)