锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲大成色www永久网站,亚洲中文无码av永久,国产亚洲精品影视在线http://www.tkk7.com/decorator/zh-cnMon, 12 May 2025 06:52:20 GMTMon, 12 May 2025 06:52:20 GMT60HttpAsyncClienthttp://www.tkk7.com/decorator/archive/2012/11/26/391950.html搴勪富娓擱搴勪富娓擱Mon, 26 Nov 2012 02:46:00 GMThttp://www.tkk7.com/decorator/archive/2012/11/26/391950.htmlhttp://www.tkk7.com/decorator/comments/391950.htmlhttp://www.tkk7.com/decorator/archive/2012/11/26/391950.html#Feedback0http://www.tkk7.com/decorator/comments/commentRss/391950.htmlhttp://www.tkk7.com/decorator/services/trackbacks/391950.html

HttpAsyncClient 鏄竴涓紓姝ョ殑 HTTP 瀹㈡埛绔紑鍙戝寘錛屽熀浜?HttpCore NIO 鍜?HttpClient 緇勪歡銆?/span>
HttpAsyncClient 鐨勫嚭鐜板茍涓嶆槸涓轟簡鏇挎崲 HttpClient錛岃屾槸浣滀負(fù)涓涓ˉ鍏呯敤浜庨渶瑕佸ぇ閲忓茍鍙戣繛鎺ワ紝瀵規(guī)ц兘瑕佹眰闈炲父楂樼殑鍩轟簬HTTP鐨勫師鐢熸暟鎹氫俊錛岃屼笖鎻愪緵浜嗕簨浠墮┍鍔ㄧ殑 API銆?br />
浠g爜紺轟緥錛?br />

       public Vector<Long> Gets(List<String> urls)
throws IOReactorException, InterruptedException {
List<HttpGet> requests = new ArrayList<HttpGet>();
for (String url : urls) {
HttpGet get = new HttpGet(url);
requests.add(get);
}
final Vector<Long> dataPackages = new Vector<Long>();
HttpAsyncClient httpclient = new DefaultHttpAsyncClient();
httpclient.getParams().setIntParameter("http.socket.timeout", 5000)
.setIntParameter("http.connection.timeout", 5000)
.setIntParameter("http.socket.buffer-size", 8192)
.setBooleanParameter("http.tcp.nodelay", true);
final CountDownLatch latch = new CountDownLatch(requests.size());
httpclient.start();
try {
for (final HttpGet request : requests) {
httpclient.execute(request, new FutureCallback<HttpResponse>() {

@Override
public void completed(HttpResponse result) {
String statInfo = "";
try {
statInfo = result.getFirstHeader("statInfo").getValue();
if (statInfo != null) {
Long size = Long.parseLong(statInfo.split(",")[0].split(":")[1]);
dataPackages.add(size);
}
} catch (Exception e) {
System.out.println(e);
System.out.println(statInfo);
}
latch.countDown();
}

@Override
public void failed(Exception ex) {
latch.countDown();
}

@Override
public void cancelled() {
latch.countDown();
}
});
}
latch.await();
} finally {
httpclient.shutdown();
}
return dataPackages;
}

 


渚濊禆鐨刾om錛?/span>httpasyncclient宸茬粡鏈?.0-beta3鐗堟湰浜?div>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpasyncclient</artifactId>
    <version>4.0-beta1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2-beta1</version>
</dependency>




]]>
web.xml欏甸潰閰嶇疆http://www.tkk7.com/decorator/archive/2012/11/13/391250.html搴勪富娓擱搴勪富娓擱Tue, 13 Nov 2012 03:37:00 GMThttp://www.tkk7.com/decorator/archive/2012/11/13/391250.htmlhttp://www.tkk7.com/decorator/comments/391250.htmlhttp://www.tkk7.com/decorator/archive/2012/11/13/391250.html#Feedback0http://www.tkk7.com/decorator/comments/commentRss/391250.htmlhttp://www.tkk7.com/decorator/services/trackbacks/391250.html闃呰鍏ㄦ枃

]]>
gc 瀵硅薄鏅嬪崌old鍖虹殑涓浜涜鍒欐葷粨http://www.tkk7.com/decorator/archive/2012/10/23/390125.html搴勪富娓擱搴勪富娓擱Tue, 23 Oct 2012 12:52:00 GMThttp://www.tkk7.com/decorator/archive/2012/10/23/390125.htmlhttp://www.tkk7.com/decorator/comments/390125.htmlhttp://www.tkk7.com/decorator/archive/2012/10/23/390125.html#Feedback0http://www.tkk7.com/decorator/comments/commentRss/390125.htmlhttp://www.tkk7.com/decorator/services/trackbacks/390125.html

ygc鍋氭楠や負(fù)錛氭竻絀篹den+from涓墍鏈塶o ref鐨勫璞″崰鐢ㄧ殑鍐呭瓨
灝唀den+from涓殑鎵鏈夊瓨?gòu)z葷殑瀵硅薄copy鍒皌o涓?/div>
鍦ㄨ繖涓繃紼嬩腑涓浜涘璞″皢鏅嬪崌鍒皁ld涓?
--to鏀句笉涓嬬殑
--瀛樻椿嬈℃暟瓚呰繃tenuring threshold鐨?/div>
閲嶆柊璁$畻Tenuring Threshold

鐒跺悗璋堣皥MaxTenuringThreshold榪欎釜鍙傛暟鐢ㄤ簬鎺у埗瀵硅薄鑳界粡鍘嗗灝戞Minor GC鎵嶆檵鍗囧埌鏃х敓浠o紝榛樿鍊兼槸15錛岄偅鏄笉鏄剰鍛崇潃瀵硅薄瑕佺粡鍘?5嬈inor gc鎵嶆檵鍗囧埌鏃х敓浠e憿錛屾潵鐪嬩笅闈㈢殑涓涓緥瀛愩?/div>

public class GCTenuringThreshold{
public static void main(String[] args) throws Exception{
GCMemoryObject object1=new GCMemoryObject(2);
GCMemoryObject object2=new GCMemoryObject(8);
GCMemoryObject object3=new GCMemoryObject(8);
GCMemoryObject object4=new GCMemoryObject(8);
object2=null;
object3=null;
GCMemoryObject object5=new GCMemoryObject(8);
Thread.sleep(4000);
object2=new GCMemoryObject(8);
object3=new GCMemoryObject(8);
object2=null;
object3=null;
object5=null;
GCMemoryObject object6=new GCMemoryObject(8);
Thread.sleep(5000);
}
}
class GCMemoryObject{
private byte[] bytes=null;
public GCMemoryObject(int multi){
bytes=new byte[1024*256*multi];
}
}

浠?Xms20M –Xmx20M –Xmn10M –XX:+UseSerialGC鍙傛暟鎵ц浠ヤ笂浠g爜錛岄氳繃jstat -gcutil [pid] 1000 10鐨勬柟寮忔煡鐪嬫墽琛屾晥鏋?/span>錛屽緢鎯婅鎵ц緇撴灉绔熺劧鏄湪絎簩嬈inor GC鐨勬椂鍊檕bject1灝辮鏅嬪崌鍒皁ld涓簡錛岃屽彲浠ヨ偗瀹氱殑鏄繖涓椂鍊?span style="word-wrap: normal; word-break: normal; color: #ff0000; ">to space絀洪棿鏄厖瓚崇殑錛屼篃灝辨槸璇村茍涓嶆槸鍦╰o space絀洪棿鍏呰凍鐨勬儏鍐典笅錛屽璞′竴瀹氳緇忓巻MaxTenuringThreshold嬈℃墠浼?xì)鏅嬪崌鍒皁ld錛?/span>閭e叿浣撹鍒欏埌搴曟槸鎬庝箞鏍風(fēng)殑鍛紝緲葷湅 Hotspot 6 update 21涓璖erialGC鐨勫疄鐜幫紝鍙互鐪嬪埌鍦ㄦ瘡嬈inor GC鍚庯紝浼?xì)瀵箻q欎釜瀛樻椿鍛ㄦ湡鐨勯槇鍊煎仛璁$畻錛岃綆楃殑浠g爜濡備笅錛?br />
size_t desired_survivor_size = (size_t)((((double) survivor_capacity)*TargetSurvivorRatio)/100);
size_t total = 0;
int age = 1;
assert(sizes[0] == 0, "no objects with age zero should be recorded");
while (age total += sizes[age];
// check if including objects of age 'age' made us pass the desired
// size, if so 'age' is the new threshold
if (total > desired_survivor_size) break;
age++;
}
int result = age
鍏朵腑desired_survivor_size鏄寚survivor space/2錛屼粠涓婇潰鐨勪唬鐮佸彲鐪嬪嚭錛屽湪璁$畻瀛樻椿鍛ㄦ湡榪欎釜闃堝兼椂錛?span style="word-wrap: normal; word-break: normal; color: #ff0000; ">hotspot浼?xì)閬嶅巻鎵鏈塧ge鐨則able錛屽茍瀵瑰叾鎵鍗犵敤鐨勫ぇ灝忚繘琛岀瘡縐紝褰撶瘡縐殑澶?灝忚秴榪囦簡survivor space鐨勪竴鍗婃椂錛屽垯浠ヨ繖涓猘ge浣滀負(fù)鏂扮殑瀛樻椿鍛ㄦ湡闃堝鹼紝鏈鍚庡彇age鍜孧axTenuringThreshold涓洿灝忕殑涓涓箋?/span>

鎸夌収榪欐牱鐨勮鍒欙紝涓婇潰鐨勮繍琛屾晥鏋滃氨鍙獙璇佷簡錛岀涓嬈inor gc鐨勬椂鍊欏瓨?gòu)z誨懆鏈熺殑闃堝間負(fù)MaxTenuringThreshold錛宮inor gc緇撴潫鍚庤綆楀嚭鏂扮殑闃堝間負(fù)1錛屽湪絎簩嬈inor gc鏃秓bject 1鐨刟ge宸茬粡鏄?浜嗭紝鍥犳object1琚檵鍗囧埌浜嗘棫鐢熶唬銆?/p>

榪欎釜瑙勫垯瀵逛簬Serial GC浠ュ強(qiáng)ParNew GC錛堜絾瀵逛簬寮鍚簡UseAdaptiveSizePolicy鐨凱arNew GC鑰岃█涔熸棤鏁堬紝榛樿鏄笉寮鍚殑錛夊潎鏈夋晥錛屽浜嶱S錛圥arallel Scavenge錛?GC鑰岃█錛?span style="word-wrap: normal; word-break: normal; color: #ff0000; ">鍦ㄩ粯璁ょ殑鎯呭喌涓嬬涓嬈′互InitialTenuringThreshold錛堥粯璁や負(fù)7錛変負(fù)鍑嗭紝涔嬪悗鍦ㄦ瘡嬈inor GC鍚庡潎浼?xì)鍔ㄦ佽綆楋紝瑙勫垯姣斾笂闈㈢殑澶嶆潅錛屽湪璁劇疆-XX:-UseAdaptiveSizePolicy鍚庯紝鍒欎互 MaxTenuringThrehsold涓哄噯錛屽茍涓斾笉浼?xì)閲嶆柊璁〗帡锛屼細(xì)鏄亽瀹氬箋?/p>

濡傚笇鏈涜窡韙瘡嬈inor GC鍚庢柊鐨勫瓨?gòu)z誨懆鏈熺殑闃堝鹼紝鍙湪鍚姩鍙傛暟涓婂鍔狅細(xì)-XX:+PrintTenuringDistribution錛岃緭鍑虹殑淇℃伅涓殑錛?br />Desired survivor size 1048576 bytes, new threshold 7 (max 15)
new threshold 7鍗蟲爣璇嗘柊鐨勫瓨?gòu)z誨懆鏈熺殑闃堝間負(fù)7銆?/p>



]]>java搴忓垪鍖栨満鍒?/title><link>http://www.tkk7.com/decorator/archive/2012/09/03/386821.html</link><dc:creator>搴勪富娓擱</dc:creator><author>搴勪富娓擱</author><pubDate>Sun, 02 Sep 2012 16:48:00 GMT</pubDate><guid>http://www.tkk7.com/decorator/archive/2012/09/03/386821.html</guid><wfw:comment>http://www.tkk7.com/decorator/comments/386821.html</wfw:comment><comments>http://www.tkk7.com/decorator/archive/2012/09/03/386821.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/decorator/comments/commentRss/386821.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/decorator/services/trackbacks/386821.html</trackback:ping><description><![CDATA[<div><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">鍘熸枃閾炬帴錛?a >http://java.sun.com/developer/technicalArticles/Programming/serialization/<br /></a><br />We all know the Java platform allows us to create reusable objects in memory. However, all of those objects exist only as long as the Java virtual machine<a style="color: #666666; "><sup>1</sup></a> remains running. It would be nice if the objects we create could exist beyond the lifetime of the virtual machine, wouldn't it? Well, with object serialization, you can flatten your objects and reuse them in powerful ways.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. The Java Serialization API provides a standard mechanism for developers to handle object serialization. The API is small and easy to use, provided the classes and methods are understood.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Throughout this article, we'll examine how to persist your Java objects, starting with the basics and proceeding to the more advanced concepts. We'll learn three different ways to perform serialization -- using the default protocol, customizing the default protocol, and creating our own protocol -- and we'll investigate concerns that arise with any persistence scheme such as object caching, version control, and performance issues.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">By the conclusion of this article, you should have a solid comprehension of that powerful yet sometimes poorly understood Java API.</p><h3>First Things First: The Default Mechanism</h3><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Let's start with the basics. To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.io.Serializable</code> interface, which signifies to the underlying API that the object can be flattened into bytes and subsequently inflated in the future.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Let's look at a persistent class we'll use to demonstrate the serialization mechanism:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 import java.io.Serializable;<br />20 import java.util.Date;<br />30 import java.util.Calendar;<br />40 public class PersistentTime <strong>implements Serializable</strong><br />50 {<br />60 private Date time;<br />70 <br />80 public PersistentTime()<br />90 {<br />100      time = Calendar.getInstance().getTime();<br />110    }<br />120<br />130    public Date getTime()<br />140    {<br />150      return time;<br />160    }<br />170  }<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">As you can see, the only thing we had to do differently from creating a normal class is implement the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.io.Serializable</code> interface on line 40. The completely empty <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> is only a <em>marker</em> interface -- it simply allows the serialization mechanism to verify that the class is able to be persisted. Thus, we turn to the first rule of serialization:</p><strong style="font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><p style="margin-top: 3px; margin-bottom: 17px; ">Rule #1: The object to be persisted must implement the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> interface or inherit that implementation from its object hierarchy.</p></strong><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">The next step is to actually persist the object. That is done with the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.io.ObjectOutputStream</code> class. That class is a <em>filter stream</em>--it is wrapped around a lower-level byte stream (called a <em>node stream</em>) to handle the serialization protocol for us. Node streams can be used to write to file systems or even across sockets. That means we could easily transfer a flattened object across a network wire and have it be rebuilt on the other side!</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Take a look at the code used to save the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">PersistentTime</code> object:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 import java.io.ObjectOutputStream;<br />20 import java.io.FileOutputStream;<br />30 import java.io.IOException;<br />40 public class FlattenTime<br />50 {<br />60 public static void main(String [] args)<br />70 {<br />80 String filename = "time.ser";<br />90 if(args.length > 0)<br />100     {<br />110       filename = args[0];<br />120     } <br />130     PersistentTime time = new PersistentTime();<br />140     FileOutputStream fos = null;<br />150     ObjectOutputStream out = null;<br />160     try<br />170     {<br />180       fos = new FileOutputStream(filename);<br />190       out = new ObjectOutputStream(fos);<br />200       out.writeObject(time);<br />210       out.close();<br />220     }<br />230     catch(IOException ex)<br />240     {<br />250       ex.printStackTrace();<br />260     }<br />270   }<br />280 }<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">The real work happens on line 200 when we call the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectOutputStream.writeObject()</code> method, which kicks off the serialization mechanism and the object is flattened (in that case to a file).</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">To restore the file, we can employ the following code:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 import java.io.ObjectInputStream;<br />20 import java.io.FileInputStream;<br />30 import java.io.IOException;<br />40 import java.util.Calendar;<br />50 public class InflateTime<br />60 {<br />70 public static void main(String [] args)<br />80 {<br />90 String filename = "time.ser"; <br />100     if(args.length > 0)<br />110     {<br />120       filename = args[0];<br />130     }<br />140   PersistentTime time = null;<br />150   FileInputStream fis = null;<br />160   ObjectInputStream in = null;<br />170   try<br />180   {<br />190     fis = new FileInputStream(filename);<br />200     in = new ObjectInputStream(fis);<br />210     time = (PersistentTime)in.readObject();<br />220     in.close();<br />230   }<br />240   catch(IOException ex)<br />250   {<br />260     ex.printStackTrace();<br />270   }<br />280   catch(ClassNotFoundException ex)<br />290   {<br />300     ex.printStackTrace();<br />310   }<br />320   // print out restored time<br />330   System.out.println("Flattened time: " + time.getTime());<br />340   System.out.println();<br />350      // print out the current time<br />360   System.out.println("Current time: " + Calendar.getInstance().getTime());<br />370 }<br />380}<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">In the code above, the object's restoration occurs on line 210 with the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectInputStream.readObject()</code> method call. The method call reads in the raw bytes that we previously persisted and creates a live object that is an exact replica of the original. Because <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">readObject()</code> can read any serializable object, a cast to the correct type is required. With that in mind, the class file must be accessible from the system in which the restoration occurs. In other words, the object's class file and methods are not saved; only the object's <em>state</em> is saved.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Later, on line 360, we simply call the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">getTime()</code> method to retrieve the time that the original object flattened. The flatten time is compared to the current time to demonstrate that the mechanism indeed worked as expected.</p><h4>Nonserializable Objects</h4><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">The basic mechanism of Java serialization is simple to use, but there are some more things to know. As mentioned before, only objects marked<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> can be persisted. The <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.lang.Object</code> class does not implement that interface. Therefore, not all the objects in Java can be persisted automatically. The good news is that most of them -- like AWT and Swing GUI components, strings, and arrays -- are serializable.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">On the other hand, certain system-level classes such as <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Thread</code>, <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">OutputStream</code> and its subclasses, and <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Socket</code> are not serializable. Indeed, it would not make any sense if they were. For example, thread running in my JVM would be using my system's memory. Persisting it and trying to run it in your JVM would make no sense at all. Another important point about <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.lang.Object</code> not implementing the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> interface is that any class you create that extends only <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Object</code> (and no other serializable classes) is not serializable unless you implement the interface yourself (as done with the previous example).</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">That situation presents a problem: what if we have a class that contains an instance of <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Thread</code>? In that case, can we ever persist objects of that type? The answer is yes, as long as we tell the serialization mechanism our intentions by marking our class's <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Thread</code> object as <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">transient</code>.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Let's assume we want to create a class that performs an animation. I will not actually provide the animation code here, but here is the class we'll use:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 import java.io.Serializable;<br />20 public class PersistentAnimation implements Serializable, Runnable<br />30 {<br />40 <strong>transient</strong> private Thread animator;<br />50 private int animationSpeed;<br />60 public PersistentAnimation(int animationSpeed)<br />70 {<br />80 this.animationSpeed = animationSpeed;<br />90 animator = new Thread(this);<br />100     animator.start();<br />110   }<br />120       public void run()<br />130   {<br />140     while(true)<br />150     {<br />160       // do animation here<br />170     }<br />180   } <br />190 }<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">When we create an instance of the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">PersistentAnimation</code> class, the thread <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">animator</code> will be created and started as we expect. We've marked the thread on line 40 <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">transient</code> to tell the serialization mechanism that the field should not be saved along with the rest of that object's state (in that case, the field <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">speed</code>). The bottom line: you must mark <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">transient</code> any field that either cannot be serialized or any field you do not want serialized. Serialization does not care about access modifiers such as <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">private</code> -- all nontransient fields are considered part of an object's persistent state and are eligible for persistence.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Therefore, we have another rule to add. Here are both rules concerning persistent objects:</p><ul style="margin-left: 13px; padding-left: 0px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><strong>Rule #1: The object to be persisted must implement the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> interface or inherit that implementation from its object hierarchy</strong></li><p style="margin-top: 3px; margin-bottom: 17px; "></p><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><strong>Rule #2: The object to be persisted must mark all nonserializable fields <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">transient</code></strong></li><p style="margin-top: 3px; margin-bottom: 17px; "></p></ul><h3>Customize the Default Protocol</h3><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Let's move on to the second way to perform serialization: customize the default protocol. Though the animation code above demonstrates how a thread could be included as part of an object while still making that object be serializable, there is a major problem with it if we recall how Java creates objects. To wit, when we create an object with the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">new</code> keyword, the object's constructor is called only when a new instance of a class is created. Keeping that basic fact in mind, let's revisit our animation code. First, we instantiate an object of type <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">PersistentAnimation</code>, which begins the animation thread sequence. Next, we serialize the object with that code:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">PersistentAnimation animation = new PersistentAnimation(10);<br />FileOutputStream fos = ...<br />ObjectOutputStream out = new ObjectOutputStream(fos);<br />out.<strong>writeObject(animation)</strong>;<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">All seems fine until we read the object back in with a call to the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">readObject()</code> method. Remember, a constructor is called only when a new instance is created. We are not creating a new instance here, we are restoring a persisted object. The end result is the animation object will work only once, when it is first instantiated. Kind of makes it useless to persist it, huh?</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Well, there is good news. We can make our object work the way we want it to; we can make the animation restart upon restoration of the object. To accomplish that, we could, for example, create a <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">startAnimation()</code> helper method that does what the constructor currently does. We could then call that method from the constructor, after which we read the object back in. Not bad, but it introduces more complexity. Now, anyone who wants to use that animation object will have to know that method has to be called following the normal deserialization process. That does not make for a seamless mechanism, something the Java Serialization API promises developers.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">There is, however, a strange yet crafty solution. By using a built-in feature of the serialization mechanism, developers can enhance the normal process by providing two methods inside their class files. Those methods are:</p><ul style="margin-left: 13px; padding-left: 0px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">private void writeObject(ObjectOutputStream out) throws IOException;</code></li><p style="margin-top: 3px; margin-bottom: 17px; "></p><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;</code></li><p style="margin-top: 3px; margin-bottom: 17px; "></p></ul><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Notice that both methods are (and must be) declared <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">private</code>, proving that neither method is inherited and overridden or overloaded. The trick here is that the virtual machine will automatically check to see if either method is declared during the corresponding method call. The virtual machine can call private methods of your class whenever it wants but no other objects can. Thus, the integrity of the class is maintained and the serialization protocol can continue to work as normal. The serialization protocol is always used the same way, by calling either<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectOutputStream.writeObject()</code> or <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectInputStream.readObject()</code>. So, even though those specialized private methods are provided, the object serialization works the same way as far as any calling object is concerned.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Considering all that, let's look at a revised version of <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">PersistentAnimation</code> that includes those private methods to allow us to have control over the deserialization process, giving us a pseudo-constructor:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 import java.io.Serializable;<br />20 public class PersistentAnimation implements Serializable, Runnable<br />30 {<br />40 <strong>transient</strong> private Thread animator;<br />50 private int animationSpeed;<br />60 public PersistentAnimation(int animationSpeed)<br />70 {<br />80 this.animationSpeed = animationSpeed;<br />90 startAnimation(); <br />100   }<br />110       public void run()<br />120   {<br />130     while(true)<br />140     {<br />150       // do animation here<br />160     }<br />170   } <br />180   private void writeObject(ObjectOutputStream out) throws IOException<br />190   {<br />200     out.defaultWriteObject(); <br />220   }<br />230   private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException<br />240   {<br />250     // our "pseudo-constructor"<br />260     in.defaultReadObject();<br />270     // now we are a "live" object again, so let's run rebuild and start<br />280     startAnimation();<br />290<br />300   }<br />310   private void startAnimation()<br />320   {<br />330     animator = new Thread(this);<br />340     animator.start();<br />350   }<br />360 }<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Notice the first line of each of the new private methods. Those calls do what they sound like -- they perform the default writing and reading of the flattened object, which is important because we are not replacing the normal process, we are only adding to it. Those methods work because the call to <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectOutputStream.writeObject()</code> kicks off the serialization protocol. First, the object is checked to ensure it implements<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> and then it is checked to see whether either of those private methods are provided. If they are provided, the stream class is passed as the parameter, giving the code control over its usage.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Those private methods can be used for any customization you need to make to the serialization process. Encryption could be added to the output and decryption to the input (note that the bytes are written and read in cleartext with no obfuscation at all). They could be used to add extra data to the stream, perhaps a company versioning code. The possibilities are truly limitless.</p><h4>Stop That Serialization!</h4><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">OK, we have seen quite a bit about the serialization process, now let's see some more. What if you create a class whose superclass is serializable but you do not want that new class to be serializable? You cannot unimplement an interface, so if your superclass does implement<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code>, your new class implements it, too (assuming both rules listed above are met). To stop the automatic serialization, you can once again use the private methods to just throw the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">NotSerializableException</code>. Here is how that would be done:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 private void writeObject(ObjectOutputStream out) throws IOException<br />20 {<br />30 throw new NotSerializableException("Not today!");<br />40 }<br />50 private void readObject(ObjectInputStream in) throws IOException<br />60 {<br />70 throw new NotSerializableException("Not today!");<br />80 }<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Any attempt to write or read that object will now always result in the exception being thrown. Remember, since those methods are declared<code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">private</code>, nobody could modify your code without the source code available to them -- no overriding of those methods would be allowed by Java.</p><h3>Create Your Own Protocol: the Externalizable Interface</h3><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Our discussion would be incomplete not to mention the third option for serialization: create your own protocol with the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Externalizable</code> interface. Instead of implementing the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> interface, you can implement <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Externalizable</code>, which contains two methods:</p><ul style="margin-left: 13px; padding-left: 0px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">public void writeExternal(ObjectOutput out) throws IOException;</code></li><p style="margin-top: 3px; margin-bottom: 17px; "></p><p style="margin-top: 3px; margin-bottom: 17px; "></p><li style="margin-left: 13px; padding-left: 0px; list-style-image: url(http://java.sun.com/im/ul_bullet.gif); "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;</code></li><p style="margin-top: 3px; margin-bottom: 17px; "></p></ul><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Just override those methods to provide your own protocol. Unlike the previous two serialization variations, nothing is provided for free here, though. That is, the protocol is entirely in your hands. Although it's the more difficult scenario, it's also the most controllable. An example situation for that alternate type of serialization: read and write PDF files with a Java application. If you know how to write and read PDF (the sequence of bytes required), you could provide the PDF-specific protocol in the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">writeExternal</code> and <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">readExternal</code> methods.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Just as before, though, there is no difference in how a class that implements <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Externalizable</code> is used. Just call <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">writeObject()</code> or <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">readObject</code>and, voila, those externalizable methods will be called automatically.</p><h3>Gotchas</h3><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">There are a few things about the serialization protocol that can seem very strange to developers who are not aware. Of course, that is the purpose of the article -- to get you aware! So let's discuss a few of those gotchas and see if we can understand why they exist and how to handle them.</p><h4>Caching Objects in the Stream</h4><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">First, consider the situation in which an object is written to a stream and then written again later. By default, an <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectOutputStream</code> 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:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">10 ObjectOutputStream out = new ObjectOutputStream(...);<br />20 MyObject obj = new MyObject(); // must be Serializable<br />30 obj.setState(100);<br />40 out.writeObject(obj); // saves object with state = 100<br />50 obj.setState(200);<br />60 out.writeObject(obj); // does not save new object state<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">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 <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">ObjectOutputStream.reset()</code> 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 <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">reset</code> flushes the entire object cache, so all objects that have been written could be rewritten.</p><h4>Version Control</h4><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">With our second gotcha, imagine you create a class, instantiate it, and write it out to an object stream. That flattened object sits in the file system for some time. Meanwhile, you update the class file, perhaps adding a new field. What happens when you try to read in the flattened object?</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Well, the bad news is that an exception will be thrown -- specifically, the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">java.io.InvalidClassException</code> -- because all persistent-capable classes are automatically given a unique identifier. If the identifier of the class does not equal the identifier of the flattened object, the exception will be thrown. However, if you really think about it, why should it be thrown just because I added a field? Couldn't the field just be set to its default value and then written out next time?</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Yes, but it takes a little code manipulation. The identifier that is part of all classes is maintained in a field called <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">serialVersionUID</code>. If you wish to control versioning, you simply have to provide the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">serialVersionUID</code> field manually and ensure it is always the same, no matter what changes you make to the classfile. You can use a utility that comes with the JDK distribution called <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">serialver</code> to see what that code would be by default (it is just the hash code of the object by default).</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Here is an example of using <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">serialver</code> with a class called <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Baz</code>:</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; "><code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">> serialver Baz<br />> Baz: static final long serialVersionUID = 10275539472837495L;<br /></code></p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Simply copy the returned line with the version ID and paste it into your code. (On a Windows box, you can run that utility with the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">- show</code> option to simplify the copy and paste procedure.) Now, if you make any changes to the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Baz</code> class file, just ensure that same version ID is specified and all will be well.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">The version control works great as long as the changes are compatible. Compatible changes include adding or removing a method or a field. Incompatible changes include changing an object's hierarchy or removing the implementation of the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Serializable</code> interface. A complete list of compatible and incompatible changes is given in the Java Serialization Specification.</p><h4>Performance Considerations</h4><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Our third gotcha: the default mechanism, although simple to use, is not the best performer. I wrote out a <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Date</code> object to a file 1,000 times, repeating that procedure 100 times. The average time to write out the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Date</code> object was 115 milliseconds. I then manually wrote out the <code style="font-family: Monaco, Courier, 'Courier New', monospace; color: #444444; ">Date</code>object, using standard I/O the same number of iterations; the average time was 52 milliseconds. Almost half the time! There is often a trade-off between convenience and performance, and serialization proves no different. If speed is the primary consideration for your application, you may want to consider building a custom protocol.</p><p style="margin-top: 3px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Another consideration concerns the aforementioned fact that object references are cached in the output stream. Due to that, the system may not garbage collect the objects written to a stream if the stream is not closed. The best move, as always with I/O, is to close the streams as soon as possible, following the write operations.</p><h3>Conclusion</h3><p style="margin-top: 2px; margin-bottom: 17px; font-family: Arial, Helvetica, FreeSans, Luxi-sans, 'Nimbus Sans L', sans-serif; font-size: 12px; line-height: normal; background-color: #ffffff; ">Serialization in Java is simple to instigate and almost as simple to implement. Understanding the three different ways of implementing serialization should aid in bending the API to your will. We have seen a lot of the serialization mechanism in that article, and I hope it made things clearer and not worse. The bottom line, as with all coding, is to maintain common sense within the bounds of API familiarity. That article has laid out a strong basis of understanding the Java Serialization API, but I recommend perusing the specification to discover more fine-grained details.</p></div><img src ="http://www.tkk7.com/decorator/aggbug/386821.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/decorator/" target="_blank">搴勪富娓擱</a> 2012-09-03 00:48 <a href="http://www.tkk7.com/decorator/archive/2012/09/03/386821.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>姝e垯琛ㄨ揪寮忚鍒?/title><link>http://www.tkk7.com/decorator/archive/2012/08/30/386631.html</link><dc:creator>搴勪富娓擱</dc:creator><author>搴勪富娓擱</author><pubDate>Thu, 30 Aug 2012 13:29:00 GMT</pubDate><guid>http://www.tkk7.com/decorator/archive/2012/08/30/386631.html</guid><wfw:comment>http://www.tkk7.com/decorator/comments/386631.html</wfw:comment><comments>http://www.tkk7.com/decorator/archive/2012/08/30/386631.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/decorator/comments/commentRss/386631.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/decorator/services/trackbacks/386631.html</trackback:ping><description><![CDATA[     鎽樿: 姝e垯琛ㄨ揪寮? 寮曡█     姝e垯琛ㄨ揪寮忥紙regular expression錛夊氨鏄敤涓涓?#8220;瀛楃涓?#8221;鏉ユ弿榪頒竴涓壒寰侊紝鐒跺悗鍘婚獙璇佸彟涓涓?#8220;瀛楃涓?#8221;鏄惁絎﹀悎榪欎釜鐗瑰緛銆傛瘮濡傝〃杈懼紡“ab+” 鎻忚堪鐨勭壒寰佹槸“涓涓?nbsp;'a' 鍜?nbsp;浠繪剰涓?nbsp;'b...  <a href='http://www.tkk7.com/decorator/archive/2012/08/30/386631.html'>闃呰鍏ㄦ枃</a><img src ="http://www.tkk7.com/decorator/aggbug/386631.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/decorator/" target="_blank">搴勪富娓擱</a> 2012-08-30 21:29 <a href="http://www.tkk7.com/decorator/archive/2012/08/30/386631.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p> <a href="http://www.tkk7.com/" title="亚洲av成人片在线观看">亚洲av成人片在线观看</a> <div class="friend-links"> </div> </div> </footer> 主站蜘蛛池模板: <a href="http://477077.com" target="_blank">啦啦啦高清视频在线观看免费</a>| <a href="http://66cws.com" target="_blank">亚洲AV无码一区二区二三区入口</a>| <a href="http://ycjs999.com" target="_blank">亚洲国产精品福利片在线观看</a>| <a href="http://sznsfe.com" target="_blank">亚洲国产激情在线一区</a>| <a href="http://www876444.com" target="_blank">成年在线网站免费观看无广告</a>| <a href="http://zuahowan.com" target="_blank">亚洲成年轻人电影网站www</a>| <a href="http://gttest5.com" target="_blank">永久免费AV无码国产网站</a>| <a href="http://7t53.com" target="_blank">日亚毛片免费乱码不卡一区</a>| <a href="http://meinvtaotu.com" target="_blank">亚洲av中文无码乱人伦在线播放</a>| <a href="http://888xxss.com" target="_blank">av午夜福利一片免费看久久</a>| <a href="http://scbangde.com" target="_blank">亚洲综合色丁香麻豆</a>| <a href="http://513109.com" target="_blank">四虎影视永久免费观看地址</a>| <a href="http://cih60o.com" target="_blank">久久久久国产精品免费免费不卡</a>| <a href="http://caopropp.com" target="_blank">一级毛片直播亚洲</a>| <a href="http://www19977.com" target="_blank">色猫咪免费人成网站在线观看 </a>| <a href="http://222222se.com" target="_blank">99久热只有精品视频免费看</a>| <a href="http://77tok.com" target="_blank">亚洲一线产品二线产品</a>| <a href="http://jjv5.com" target="_blank">国产亚洲大尺度无码无码专线</a>| <a href="http://2xpp.com" target="_blank">羞羞漫画在线成人漫画阅读免费</a>| <a href="http://51708695.com" target="_blank">日韩激情无码免费毛片</a>| <a href="http://zzhjnmzp.com" target="_blank">很黄很污的网站免费</a>| <a href="http://btztjxc.com" target="_blank">久久精品国产亚洲AV未满十八</a>| <a href="http://czshenyue.com" target="_blank">香蕉高清免费永久在线视频 </a>| <a href="http://by8847.com" target="_blank">一区二区三区四区免费视频</a>| <a href="http://cc006.com" target="_blank">国产精品亚洲一区二区三区在线观看</a>| <a href="http://eldadhagar.com" target="_blank">女人被男人桶得好爽免费视频 </a>| <a href="http://ghdc5.com" target="_blank">中文字幕免费播放</a>| <a href="http://scbangde.com" target="_blank">国产亚洲sss在线播放</a>| <a href="http://0515zs.com" target="_blank">国产亚洲综合一区柠檬导航</a>| <a href="http://bisi666.com" target="_blank">蜜桃视频在线观看免费网址入口</a>| <a href="http://jdv6.com" target="_blank">亚洲久热无码av中文字幕</a>| <a href="http://linanhotel.com" target="_blank">亚洲ⅴ国产v天堂a无码二区</a>| <a href="http://tsxyhq.com" target="_blank">免费又黄又爽又猛的毛片</a>| <a href="http://19520888.com" target="_blank">亚洲视频在线免费看</a>| <a href="http://wcnmgb.com" target="_blank">a视频在线免费观看</a>| <a href="http://zjztauto.com" target="_blank">免费无码午夜福利片</a>| <a href="http://taojin5.com" target="_blank">中文字幕 亚洲 有码 在线 </a>| <a href="http://868664.com" target="_blank">亚洲一卡一卡二新区无人区</a>| <a href="http://selangcun.com" target="_blank">亚洲A∨无码无在线观看</a>| <a href="http://aaaaa123.com" target="_blank">亚洲国产一成久久精品国产成人综合</a>| <a href="http://666fzw.com" target="_blank">国产精品九九久久免费视频</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>