﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-自大的人只想着怎么把面子保住，自强的人只想着怎么把事办好！-文章分类-CoreJava</title><link>http://www.blogjava.net/mlzry0612/category/22478.html</link><description>自大的人只想着怎么把面子保住，自强的人只想着怎么把事办好！</description><language>zh-cn</language><lastBuildDate>Sat, 06 Apr 2013 06:08:48 GMT</lastBuildDate><pubDate>Sat, 06 Apr 2013 06:08:48 GMT</pubDate><ttl>60</ttl><item><title>Java Path</title><link>http://www.blogjava.net/mlzry0612/articles/397400.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Fri, 05 Apr 2013 05:38:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/397400.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/397400.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/397400.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/397400.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/397400.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 这两天又做了个Execute Jar Project.觉得以前的Path的知识都忘的差不多了，决定自己总结一下。1. 获取类路径&nbsp;&nbsp;&nbsp;&nbsp;返回类路径内容，一般为：lib1.jar;lib2.jar。1System.getProperty("java.class.path");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在命令行运行Java程序时...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/397400.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/397400.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2013-04-05 13:38 <a href="http://www.blogjava.net/mlzry0612/articles/397400.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Algorithms Chapter2 Answer</title><link>http://www.blogjava.net/mlzry0612/articles/Algorithms.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Mon, 23 Jul 2012 14:12:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/Algorithms.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/383795.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/Algorithms.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/383795.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/383795.html</trackback:ping><description><![CDATA[<p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">2.1-1这题可以参照书上17自己给出过程，这里就略去了。</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">2.1-2 先给出书上insertion-sort的C源代码吧，然后再给出按照非升序的代码：</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">课本中(非降序的)insertion-sort代码：</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><pre style="padding: 0px; margin-top: 0px; background-color: #f7f7f7; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 710px; color: #555555; "><ol style="padding: 5px 0px; margin: 0px 0px 1px 3em !important; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; color: #5c5c5c; list-style-position: initial; list-style-image: initial; "><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; "><span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">void</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;insertion_sort(</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*A,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;n)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;i,j;&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;key;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">for</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i++)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;i&nbsp;-&nbsp;1;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;=&nbsp;A[i];&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">while</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(j&nbsp;&gt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;A[j]&nbsp;&gt;&nbsp;key)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[j+1]&nbsp;=&nbsp;A[j];&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;j&nbsp;-&nbsp;1;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[j+1]&nbsp;=&nbsp;key;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">}&nbsp;</span></li></ol></pre><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">在这题中，只要讲非降序改成非升序排序，所以改后代码如下：</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><pre style="padding: 0px; margin-top: 0px; background-color: #f7f7f7; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 710px; color: #555555; "><ol style="padding: 5px 0px; margin: 0px 0px 1px 3em !important; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; color: #5c5c5c; list-style-position: initial; list-style-image: initial; "><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; "><span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">void</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;insertion_sort(</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*A,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;n)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;i,j;&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;key;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">for</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i++)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;i&nbsp;-&nbsp;1;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;=&nbsp;A[i];&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">while</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(j&nbsp;&gt;=&nbsp;0&nbsp;&amp;&amp;&nbsp;A[j]&nbsp;&lt;&nbsp;key)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[j+1]&nbsp;=&nbsp;A[j];&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;j&nbsp;-&nbsp;1;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A[j+1]&nbsp;=&nbsp;key;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">}&nbsp;</span></li></ol></pre><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">2.1-3这题给出伪代码：</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><pre style="padding: 0px; margin-top: 0px; background-color: #f7f7f7; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 710px; color: #555555; "><ol style="padding: 5px 0px; margin: 0px 0px 1px 3em !important; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; color: #5c5c5c; list-style-position: initial; list-style-image: initial; "><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; "><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;find(</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*A&nbsp;,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;n,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;v)&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">{&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;&nbsp;i&nbsp;=&nbsp;0;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">for</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(&nbsp;;&nbsp;i&nbsp;&lt;&nbsp;n;&nbsp;i++)&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">if</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(v&nbsp;==&nbsp;A[i])&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">return</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;i;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">return</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;NIL;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">}&nbsp;</span></li></ol></pre><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">2.1-4直接给出代码：</p><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><pre style="padding: 0px; margin-top: 0px; background-color: #f7f7f7; font-family: 'Courier New', monospace; font-size: 12px; overflow: auto; width: 710px; color: #555555; "><ol style="padding: 5px 0px; margin: 0px 0px 1px 3em !important; border-left-width: 3px; border-style: none none none solid; border-left-color: #146b00; color: #5c5c5c; list-style-position: initial; list-style-image: initial; "><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; "><span style="padding: 0px; margin: 0px; border: none; color: #008200; background-color: inherit; ">/*在A[]和B[]中，数组的最低位对应与二进制的高位，即如果一个二进制数是011100，用数组表示就是A[]&nbsp;=&nbsp;{0,1,1,1,0,0}*/</span>&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; "><span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">void</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;add(</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*A&nbsp;,</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*B,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;*C,&nbsp;</span><span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;n)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;i,&nbsp;a,&nbsp;c&nbsp;=&nbsp;0;&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #2e8b57; background-color: inherit; font-weight: bold; ">int</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">&nbsp;s;&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;<span style="padding: 0px; margin: 0px; border: none; color: #006699; background-color: inherit; font-weight: bold; ">for</span><span style="padding: 0px; margin: 0px; border: none; background-color: inherit; ">(i&nbsp;=&nbsp;n&nbsp;-&nbsp;1;&nbsp;i&nbsp;&gt;= 0&nbsp;;&nbsp;i--)&nbsp;&nbsp;</span></span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;A[i]&nbsp;+&nbsp;B[i];&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C[i+1]&nbsp;=&nbsp;(s&nbsp;+&nbsp;c)&nbsp;%&nbsp;2;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;(s&nbsp;+&nbsp;c)&nbsp;/&nbsp;2;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; ">&nbsp;</li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; color: inherit; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">&nbsp;&nbsp;&nbsp;&nbsp;C[0]&nbsp;=&nbsp;c;&nbsp;&nbsp;</span></li><li style="padding: 0px 3px 0px 10px !important; margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 0px !important; margin-left: 20px; list-style: decimal-leading-zero outside; line-height: 1.3em; border: none; "><span style="padding: 0px; margin: 0px; border: none; color: black; background-color: inherit; ">}&nbsp;</span></li></ol></pre><p style="padding: 0px 0px 15px; margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; font-size: 14px; line-height: 28px; background-color: #ffffff; ">&nbsp;</p><img src ="http://www.blogjava.net/mlzry0612/aggbug/383795.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2012-07-23 22:12 <a href="http://www.blogjava.net/mlzry0612/articles/Algorithms.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>优化屏障和内存屏障</title><link>http://www.blogjava.net/mlzry0612/articles/381414.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Mon, 25 Jun 2012 04:05:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/381414.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/381414.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/381414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/381414.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/381414.html</trackback:ping><description><![CDATA[<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: #4d4d4d; font-family: 'Helvetica Neue', Arial, 'Lucida Grande', 'Lucida Sans Unicode', 'Microsoft YaHei', sans-serif; line-height: 20px; text-align: left; background-color: #ffffff; "><span style="border: 0px; font-size: 18px; margin: 0px; outline: 0px; padding: 0px; "><strong style="border: 0px; font-size: 18px; margin: 0px; outline: 0px; padding: 0px; color: #2e2e2e; "><span style="border: 0px; font-size: 18px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; "><span style="border: 0px; font-size: 18px; margin: 0px; outline: 0px; padding: 0px; color: #008000; ">优化屏障和内存屏障</span></span></strong></span></p><p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: #4d4d4d; font-family: 'Helvetica Neue', Arial, 'Lucida Grande', 'Lucida Sans Unicode', 'Microsoft YaHei', sans-serif; line-height: 20px; text-align: left; background-color: #ffffff; "><strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #2e2e2e; "><span style="border: 0px; font-size: 16px; margin: 0px; outline: 0px; padding: 0px; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #0000ff; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; ">优化屏障&nbsp;</span></span></span></strong><strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #2e2e2e; "><span style="border: 0px; font-size: 16px; margin: 0px; outline: 0px; padding: 0px; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #0000ff; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; ">(Optimization Barrier)</span></span></span></strong></p><p style="border: 0px; font-size: 12px; margin: 0px 0px 9px; outline: 0px; padding: 0px; text-align: left; background-color: #ffffff; line-height: normal; font-family: song, Verdana; border-collapse: collapse; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; "><span style="border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; ">编译器编译源代码时，会将源代码进行优化，将源代码的指令进行重排序，以适合于CPU的并行执行。然而，内核同步必须避免指令重新排序，优化屏障（Optimization barrier）避免编译器的重排序优化操作，保证编译程序时在优化屏障之前的指令不会在优化屏障之后执行。<br style="font-family: song, Verdana; font-size: 12px; " /><br />Linux用宏barrier实现优化屏障，gcc编译器的优化屏障宏定义列出如下（在include/linux/compiler-gcc.h中）：&nbsp;<br style="font-family: song, Verdana; font-size: 12px; " /><br />#define barrier() __asm__ __volatile__("":&nbsp;:&nbsp;:"memory")</span></span></p><p style="border: 0px; font-size: 12px; margin: 0px 0px 9px; outline: 0px; padding: 0px; text-align: left; background-color: #ffffff; line-height: normal; font-family: song, Verdana; border-collapse: collapse; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; "><span style="border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; ">上述定义中，&#8220;__asm__&#8221;表示插入了汇编语言程序，&#8220;__volatile__&#8221;表示阻止编译器对该值进行优化，确保变量使用了用户定义的精确地址，而不是装有同一信息的一些别名。&#8220;memory&#8221;表示指令修改了内存单元。</span></span></p><p style="border: 0px; font-size: 12px; margin: 0px 0px 9px; outline: 0px; padding: 0px; text-align: left; background-color: #ffffff; line-height: normal; font-family: song, Verdana; border-collapse: collapse; "></p><p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: #4d4d4d; font-family: 'Helvetica Neue', Arial, 'Lucida Grande', 'Lucida Sans Unicode', 'Microsoft YaHei', sans-serif; line-height: 20px; text-align: left; background-color: #ffffff; "><strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #2e2e2e; "><span style="border: 0px; font-size: 16px; margin: 0px; outline: 0px; padding: 0px; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; color: #0000ff; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; ">内存屏障 (Memory Barrier)</span></span></span></strong></p><p style="border: 0px; font-size: 12px; margin: 0px 0px 9px; outline: 0px; padding: 0px; text-align: left; background-color: #ffffff; line-height: normal; font-family: song, Verdana; border-collapse: collapse; "><span style="border: 0px; margin: 0px; outline: 0px; padding: 0px; font-family: arial, helvetica, sans-serif; "><span style="border: 0px; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; ">软件可通过读写屏障强制内存访问次序。读写屏障像一堵墙，所有在设置读写屏障之前发起的内存访问，必须先于在设置屏障之后发起的内存访问之前完成，确保内存访问按程序的顺序完成。<br style="font-family: song, Verdana; font-size: 12px; " /><br />读写屏障通过处理器构架的特殊指令mfence（内存屏障）、lfence（读屏障）和sfence（写屏障）完成，见《x86-64构架规范》一章。另外，在x86-64处理器中，对硬件进行操作的汇编语言指令是&#8220;串行的&#8221;，也具有内存屏障的作用，如：对I/O端口进行操作的所有指令、带lock前缀的指令以及写控制寄存器、系统寄存器或调试寄存器的所有指令（如：cli和sti）。<br style="font-family: song, Verdana; font-size: 12px; " /><br />Linux内核提供的内存屏障API函数说明如表2。内存屏障可用于多处理器和单处理器系统，如果仅用于多处理器系统，就使用smp_xxx函数，在单处理器系统上，它们什么都不要。</span></span></p><img src ="http://www.blogjava.net/mlzry0612/aggbug/381414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2012-06-25 12:05 <a href="http://www.blogjava.net/mlzry0612/articles/381414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java concurrent 总结一</title><link>http://www.blogjava.net/mlzry0612/articles/381400.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Mon, 25 Jun 2012 01:37:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/381400.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/381400.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/381400.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/381400.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/381400.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Preface最近看了一下&lt;Java Concurrency In Practice&gt; 这本书， 总体来说还是一本不错的书， 不过粒度不够细， 是从大的角度， 例如： 设计整体项目上如何考虑并发的多方面因素，不过总体上来说还是一本不错的书，结合部分网络上的资料，总结一下自己的知识，免的忘了。下面是一些最基本的知识，不想再写了，反正网上多的是，挑了一篇还不错的转过来，大家要支持别人的成果...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/381400.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/381400.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2012-06-25 09:37 <a href="http://www.blogjava.net/mlzry0612/articles/381400.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java nio小结</title><link>http://www.blogjava.net/mlzry0612/articles/376593.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Wed, 25 Apr 2012 07:34:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/376593.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/376593.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/376593.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/376593.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/376593.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #4b4b4b; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffffff; "><h1 class="postTitle" style="font-size: 14.7px; font-weight: bold; margin-bottom: 10px; "><a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2282268.html" style="text-decoration: none; color: #1a8bc8; ">java nio小结</a></h1><div id="cnblogs_post_body"><p style="line-height: 19px; ">根据网上的资料做些整理</p><p style="line-height: 19px; "><span class="postTitle2" style="line-height: 19px; text-decoration: none; ">Java NIO API详解</span></p><p style="line-height: 19px; "><a href="http://www.blogjava.net/19851985lili/articles/93524.html" style="color: #1a8bc8; text-decoration: none; ">http://www.blogjava.net/19851985lili/articles/93524.html</a></p><p style="line-height: 19px; ">这篇文章对nio的api讲解比较全，可以帮助在宏观上把握nio。</p><p style="line-height: 19px; ">BIO 方式使得整个处理过程和连接是绑定的，只要连接建立，无论客户端是否有消息发送，都要进行等待处理，一定程度上浪费了服务器端的硬件资源，因此就有了NIO 方式。Java 对于 NIO 方式的支持是通过 Channel和 Selector 方式来实现，采用的方法为向 Channel注册感兴趣的事件，然后通过 Selector 来获取到发生了事件的 key，如发生了相应的事件，则进行相应的处理，否则则不做任何处理，是典型的Reactor 模式，按照这样的方式，就不用像 BIO 方式一样，即使在没有消息的情况下也需要占据一个线程来阻塞读取消息，从而提升服务器的使用效率， 为实现 TCP/IP+NIO 方式的系统间通讯， Java 提供了 SocketChannel和 ServerSocketChannel两个关键的类，网络 IO 的操作则改为通过ByteBuffer 来实现，具体的基于 java实现TCP/IP+NIO 方式的通讯的方法如下所示。</p><p style="line-height: 19px; ">服务器端：</p><div class="cnblogs_code" style="font-size: 12px; margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; "><div class="cnblogs_code_toolbar" style="margin-top: 5px; "><span class="cnblogs_code_copy" style="line-height: 1.5; font-size: 12px; padding-right: 5px; "><a href="http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2282268.html" title="复制代码" style="color: #1a8bc8; text-decoration: none; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; "><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; " /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px; font-family: 'Courier New'; "><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">package</span> com.eric.test.nio;</span><br /><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.io.IOException;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.net.InetSocketAddress;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.net.ServerSocket;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.ByteBuffer;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.SelectionKey;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.Selector;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.ServerSocketChannel;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.util.Iterator;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.util.Set;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.SocketChannel;</span><br /><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">class</span> NIOServer {</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">标志数字</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span></span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> flag = 0;</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">定义缓冲区大小</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span></span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> block = 4096;</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">接收缓冲区</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span></span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> ByteBuffer receiveBuffer = ByteBuffer.allocate(block);</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">发送缓冲区</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span></span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> ByteBuffer sendBuffer = ByteBuffer.allocate(block);</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">定义Selector</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span></span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> Selector selector;</span><br />    <br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> NIOServer(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> port) <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">throws</span> IOException{</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">打开服务器套接字通道</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">服务器配置为非阻塞</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        serverSocketChannel.configureBlocking(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">false</span>);</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">检索与此服务器套接字通道关联的套接字</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        ServerSocket serverSocket = serverSocketChannel.socket();</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">进行服务的绑定</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        serverSocket.bind(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">new</span> InetSocketAddress(port));</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">通过open()方法找到Selector</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        selector = Selector.open();</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">注册到selector</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);</span><br /><span style="line-height: 1.5; font-size: 14px; ">        System.out.println("Server Start -----8888:");</span><br /><span style="line-height: 1.5; font-size: 14px; ">    }</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">监听</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">void</span> listen() <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">throws</span> IOException{</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">while</span>(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">true</span>){</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">监控所有注册的 channel ，当其中有注册的 IO 操作可以进行时，该函数返回，并将对应的 SelectionKey 加入 selected-key set</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            selector.select();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">Selected-key set 代表了所有通过 select() 方法监测到可以进行 IO 操作的 channel ，这个集合可以通过 selectedKeys() 拿到</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            Set&lt;SelectionKey&gt; selectionKeys = selector.selectedKeys();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            Iterator&lt;SelectionKey&gt; iterator = selectionKeys.iterator();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">while</span>(iterator.hasNext()){</span><br /><span style="line-height: 1.5; font-size: 14px; ">                SelectionKey selectionKey = iterator.next();</span><br /><span style="line-height: 1.5; font-size: 14px; ">                handleKey(selectionKey);</span><br /><span style="line-height: 1.5; font-size: 14px; ">                iterator.remove();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            }</span><br /><span style="line-height: 1.5; font-size: 14px; ">        }</span><br />        <br /><span style="line-height: 1.5; font-size: 14px; ">    }</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">处理请求</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">void</span> handleKey(SelectionKey selectionKey) <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">throws</span> IOException{</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">接受请求</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        ServerSocketChannel serverSocketChannel = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">null</span>;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        SocketChannel socketChannel = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">null</span>;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        String receiveText;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        String sendText;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> count;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">测试此键的通道是否准备好接受新的套接字连接</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span>(selectionKey.isAcceptable()){</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">返回创建此键的通道</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            serverSocketChannel = (ServerSocketChannel)selectionKey.channel();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">接受客户端建立连接的请求，并返回 SocketChannel 对象</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel = serverSocketChannel.accept();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">配置为非阻塞</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel.configureBlocking(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">false</span>);</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">注册到selector</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel.register(selector, SelectionKey.OP_READ);</span><br /><span style="line-height: 1.5; font-size: 14px; ">        }<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">else</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span>(selectionKey.isReadable()){</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">返回为之创建此键的通道</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel = (SocketChannel)selectionKey.channel();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将缓冲区清空，以备下次读取</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            receiveBuffer.clear();</span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将发送来的数据读取到缓冲区</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            </span><br /><span style="line-height: 1.5; font-size: 14px; ">            count = socketChannel.read(receiveBuffer);</span><br />        <br />            <br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span>(count&gt;0){</span><br /><span style="line-height: 1.5; font-size: 14px; ">                receiveText = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">new</span> String(receiveBuffer.array(),0,count);</span><br /><span style="line-height: 1.5; font-size: 14px; ">                System.out.println("服务器端接受到的数据---"+receiveText);</span><br /><span style="line-height: 1.5; font-size: 14px; ">                socketChannel.register(selector, SelectionKey.OP_WRITE);</span><br /><span style="line-height: 1.5; font-size: 14px; ">            }</span><br /><span style="line-height: 1.5; font-size: 14px; ">        }<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">else</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span> (selectionKey.isWritable()) {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将缓冲区清空以备下次写入  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            sendBuffer.clear();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 返回为之创建此键的通道。  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel = (SocketChannel) selectionKey.channel();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            sendText="message from server--" + flag++;  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">向缓冲区中输入数据  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            sendBuffer.put(sendText.getBytes());  </span><br /><span style="line-height: 1.5; font-size: 14px; ">             <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            sendBuffer.flip();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">输出到通道  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            socketChannel.write(sendBuffer);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            System.out.println("服务器端向客户端发送数据--："+sendText);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            socketChannel.register(selector, SelectionKey.OP_READ);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        }  </span><br />        <br /><span style="line-height: 1.5; font-size: 14px; ">    }</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">void</span> main(String[] args) <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">throws</span> IOException {</span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> port = 8888; </span><br /><span style="line-height: 1.5; font-size: 14px; ">        NIOServer server = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">new</span> NIOServer(port);</span><br /><span style="line-height: 1.5; font-size: 14px; ">        server.listen();</span><br /><span style="line-height: 1.5; font-size: 14px; ">    }</span><br /><br /><span style="line-height: 1.5; font-size: 14px; ">}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px; "><span class="cnblogs_code_copy" style="line-height: 1.5; font-size: 12px; padding-right: 5px; "><a href="http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2282268.html" title="复制代码" style="color: #1a8bc8; text-decoration: none; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; "><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; " /></a></span></div></div><p style="line-height: 19px; ">客户端</p><div class="cnblogs_code" style="font-size: 12px; margin-top: 5px; margin-right: 0px; margin-bottom: 5px; margin-left: 0px; "><div class="cnblogs_code_toolbar" style="margin-top: 5px; "><span class="cnblogs_code_copy" style="line-height: 1.5; font-size: 12px; padding-right: 5px; "><a href="http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2282268.html" title="复制代码" style="color: #1a8bc8; text-decoration: none; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; "><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; " /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-size: 12px; font-family: 'Courier New'; "><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">package</span> com.eric.test.nio;</span><br /><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.io.IOException;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.net.InetSocketAddress;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.ByteBuffer;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.SelectionKey;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.Selector;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.nio.channels.SocketChannel;</span><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">import</span> java.util.Set;</span><br /><br /><span style="line-height: 1.5; font-size: 14px; "><span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">class</span> NIOClient {</span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">标识数字</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span>  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> flag = 0;  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">缓冲区大小</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span>  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> BLOCK = 4096;  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">接受数据缓冲区</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span>  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> ByteBuffer sendBuffer = ByteBuffer.allocate(BLOCK);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">发送数据缓冲区</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span>  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> ByteBuffer receiveBuffer = ByteBuffer.allocate(BLOCK);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">/*</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">服务器端地址</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">*/</span>  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">private</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">final</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> InetSocketAddress SERVER_ADDRESS = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">new</span> InetSocketAddress(  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            "localhost", 8888);  </span><br />  <br /><span style="line-height: 1.5; font-size: 14px; ">    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">public</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">static</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">void</span> main(String[] args) <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">throws</span> IOException {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 打开socket通道  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        SocketChannel clientChannel = SocketChannel.open();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 设置为非阻塞方式  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        clientChannel.configureBlocking(<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">false</span>);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 打开选择器  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        Selector selector = Selector.open();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 注册连接服务端socket动作  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        clientChannel.register(selector, SelectionKey.OP_CONNECT);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 连接  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>        clientChannel.connect(SERVER_ADDRESS);  </span><br />    <br /><span style="line-height: 1.5; font-size: 14px; ">        SocketChannel socketChannel;</span><br /><span style="line-height: 1.5; font-size: 14px; ">        Set&lt;SelectionKey&gt; selectionKeys;    </span><br /><span style="line-height: 1.5; font-size: 14px; ">        String receiveText;  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        String sendText;  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">int</span> count=0;  </span><br />  <br /><span style="line-height: 1.5; font-size: 14px; ">        <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">while</span> (<span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">true</span>) {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">选择一组键，其相应的通道已为 I/O 操作准备就绪。  <br />            </span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">监控所有注册的 channel ，当其中有注册的 IO 操作可以进行时，该函数返回，并将对应的 SelectionKey 加入 selected-key set </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            selector.select();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">返回此选择器的已选择键集。  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            selectionKeys = selector.selectedKeys();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">System.out.println(selectionKeys.size());  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>            <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">for</span>(SelectionKey selectionKey:selectionKeys){ </span><br /><span style="line-height: 1.5; font-size: 14px; ">                <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">判断是否为建立连接的事件</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span> (selectionKey.isConnectable()) {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    System.out.println("client connect");  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel = (SocketChannel) selectionKey.channel();  <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br />                    </span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 判断此通道上是否正在进行连接操作。  <br />                    </span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "> 完成套接字通道的连接过程。  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span> (socketChannel.isConnectionPending()) { </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">完成连接的建立（TCP三次握手）</span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                        socketChannel.finishConnect();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        System.out.println("完成连接!");  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        sendBuffer.clear();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        sendBuffer.put("Hello,Server".getBytes());  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        sendBuffer.flip();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        socketChannel.write(sendBuffer);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    }  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel.register(selector, SelectionKey.OP_READ);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                } <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">else</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span> (selectionKey.isReadable()) {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel = (SocketChannel) selectionKey.channel();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将缓冲区清空以备下次读取  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                    receiveBuffer.clear();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">读取服务器发送来的数据到缓冲区中  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                    count=socketChannel.read(receiveBuffer);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span>(count&gt;0){  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        receiveText = <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">new</span> String( receiveBuffer.array(),0,count);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        System.out.println("客户端接受服务器端数据--:"+receiveText);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                        socketChannel.register(selector, SelectionKey.OP_WRITE);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    }  </span><br />  <br /><span style="line-height: 1.5; font-size: 14px; ">                } <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">else</span> <span style="line-height: 1.5; font-size: 12px; color: #0000ff; ">if</span> (selectionKey.isWritable()) {  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    sendBuffer.clear();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel = (SocketChannel) selectionKey.channel();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    sendText = "message from client--" + (flag++);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    sendBuffer.put(sendText.getBytes());  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                     <span style="line-height: 1.5; font-size: 12px; color: #008000; ">//</span><span style="line-height: 1.5; font-size: 12px; color: #008000; ">将缓冲区各标志复位,因为向里面put了数据标志被改变要想从中读取数据发向服务器,就要复位  </span><span style="line-height: 1.5; font-size: 12px; color: #008000; "><br /></span>                    sendBuffer.flip();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel.write(sendBuffer);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    System.out.println("客户端向服务器端发送数据--："+sendText);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                    socketChannel.register(selector, SelectionKey.OP_READ);  </span><br /><span style="line-height: 1.5; font-size: 14px; ">                }  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            }  </span><br /><span style="line-height: 1.5; font-size: 14px; ">            selectionKeys.clear();  </span><br /><span style="line-height: 1.5; font-size: 14px; ">        }  </span><br /><span style="line-height: 1.5; font-size: 14px; ">    }  </span><br /><span style="line-height: 1.5; font-size: 14px; ">}</span></pre><div class="cnblogs_code_toolbar" style="margin-top: 5px; "><span class="cnblogs_code_copy" style="line-height: 1.5; font-size: 12px; padding-right: 5px; "><a href="http://www.cnblogs.com/flyoung2008/archive/2011/12/09/2282268.html" title="复制代码" style="color: #1a8bc8; text-decoration: none; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; "><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-style: initial; border-color: initial; background-color: #f5f5f5 !important; border-top-style: none !important; border-right-style: none !important; border-bottom-style: none !important; border-left-style: none !important; border-width: initial !important; border-color: initial !important; " /></a></span></div></div><p style="line-height: 19px; ">小结：之前对Selector注册事件和SocketChannel有点小困惑。SocketChannel就像一根水管，当监听到写事件时，就往管道写数据；当监听到读事件时，就从管道读出数据。</p></div></span><img src ="http://www.blogjava.net/mlzry0612/aggbug/376593.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2012-04-25 15:34 <a href="http://www.blogjava.net/mlzry0612/articles/376593.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>String--&gt;Byte[]--&gt;Image</title><link>http://www.blogjava.net/mlzry0612/articles/350562.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Thu, 19 May 2011 00:37:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/350562.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/350562.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/350562.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/350562.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/350562.html</trackback:ping><description><![CDATA[<div><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">下面就给出具体代码，与以往一样，我会在代码的适当位置<a id="vad_8" name="8" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11768&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.spider.com.cn/spider/cpsbusindex.action%3Fmethod%3Dcpshj&amp;v=0&amp;k=%u4F7F%u7528&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=272034" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">使用</a>注释。但我不会保证在我机器上可以运行的程序会在所有机器上运行（据说这可能是人品问题）。</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">import javax.microedition.lcdui.*;<br />import java.io.*;</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">public class ImageCanvas<br />&nbsp;&nbsp;&nbsp; extends Canvas<br />{<br />&nbsp; private Image byteImg;<br />&nbsp; private InputStream input;<br />&nbsp; public ImageCanvas()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; try<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jbInit();<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; catch (Exception e)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; /**<br />&nbsp;&nbsp; * 三种方法任选一种<br />&nbsp;&nbsp; * @throws Exception<br />&nbsp;&nbsp; */<br />&nbsp; private void jbInit() throws Exception<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; byteImg = this.readImage("/res/pic.bin", 11110); // 从<a id="vad_14" name="14" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11984&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.naruko.com.cn/channel/HongJie/Hongjie.aspx%3Furl%3Dhttp%3A//www.naruko.com.cn/shop/product/SearchResult.aspx%3Fb%3D0%26key%3D%25e5%2586%25bb%25e8%2586%259c%26page%3D2&amp;v=0&amp;k=%u4E00%u4E2A&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=551535" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">一个</a>合成包里读取资源，如*.bin文件<br />&nbsp;&nbsp;&nbsp; //byteImg = Image.createImage("/res/caidan.png"); //&nbsp;<a id="vad_4" name="4" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11919&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.herbuy.com.cn/Activity/CommonActivity.aspx%3Faid%3Dd2fb4514d7574b339fc93d8c773062e3&amp;v=0&amp;k=%u76F4%u63A5&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=767798" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">直接</a>读取PNG文件<br />&nbsp;&nbsp;&nbsp; //byteImg = this.importImage(this.caiDanImage()); // 从byte数组里记取资源，将资源像素保存在byte数组中<br />&nbsp;&nbsp;&nbsp; System.out.println("内存情况: " +<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Runtime.getRuntime().freeMemory() +<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " / " + Runtime.getRuntime().totalMemory());//这种情况下会<a id="vad_9" name="9" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=12005&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.herbuy.com.cn/channel/HongJie/Hongjie.aspx%3Furl%3Dhttp%3A//www.herbuy.com.cn/Activity/CommonActivity.aspx%3Faid%3D70ab805b7b9347409c94c1a84acfaa8c&amp;v=0&amp;k=%u7B80%u5355&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=395906" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">简单</a>看下每种方法对内存的影响<br />&nbsp;&nbsp;&nbsp; //this.getByte("/res/caidan.png");<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; /**<br />&nbsp;&nbsp; * Reads a file from the BIN file and return data as an Image<br />&nbsp;&nbsp; * @param binfile String 文件名<br />&nbsp;&nbsp; * @param pos long&nbsp;<a id="vad_12" name="12" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11962&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.naruko.com.cn/channel/HongJie/Hongjie.aspx%3Furl%3Dhttp%3A//www.naruko.com.cn/shop/&amp;v=0&amp;k=%u9700%u8981&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=216598" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">需要</a>跳过的字节长度<br />&nbsp;&nbsp; * @return Image&nbsp;<a id="vad_0" name="0" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11967&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//www.herbuy.com.cn/channel/HongJie/Hongjie.aspx%3Furl%3Dhttp%3A//www.herbuy.com.cn/product/product.aspx%3Fgid%3DGD1046400023&amp;v=0&amp;k=%u8FD4%u56DE&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=753275" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">返回</a>一个Image对像<br />&nbsp;&nbsp; */<br />&nbsp; public Image readImage(String binfile, long pos)<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; byte buffer[];<br />&nbsp;&nbsp;&nbsp; int len;<br />&nbsp;&nbsp;&nbsp; try<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InputStream is = getClass().getResourceAsStream(binfile);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is.skip(pos);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len = (is.read() &amp; 0xFF) &lt;&lt; 24; // 如果不懂得这几句的意思，请留言给我，或是补习一下基本的编程知识<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len |= (is.read() &amp; 0xFF) &lt;&lt; 16;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len |= (is.read() &amp; 0xFF) &lt;&lt; 8;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; len |= (is.read() &amp; 0xFF);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buffer = new byte[len];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is.read(buffer, 0, buffer.length);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; is = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; catch (Exception e)<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buffer = null;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.gc();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; return Image.createImage(buffer, 0, buffer.length);<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; /**<br />&nbsp;&nbsp; * 将资源文件<a id="vad_11" name="11" href="http://221.238.248.251/click/c1.php?r=http%3A//www.google.com.hk/url%3Fsa%3Dt%26source%3Dweb%26cd%3D7%26ved%3D0CFsQFjAG%26url%3Dhttp%253A%252F%252Fwww.sf.org.cn%252Fblog%252Fxuweidong%252Fj2me%252F200509%252F313.html%26rct%3Dj%26q%3Djava%2520%25E5%259B%25BE%25E7%2589%2587%25E8%25AF%25BB%25E5%258F%2596%26ei%3DVmXUTbilOYiovQOqzZT7BA%26usg%3DAFQjCNF_DFQHONeBBWmXzpQManSVfuZByA%26sig2%3D-EZbq_qubWW5mandyVUjxw%26cad%3Drja&amp;aid=11887&amp;sid=6235007045036118&amp;click=1&amp;url=http%3A//t.dianping.com/deal/2200%3Futm_medium%3Demailad%26utm_source%3Dhongjie%26utm_term%3Dtuangou100820%26utm_campaign%3Dad&amp;v=0&amp;k=%u8F6C%u6362&amp;s=http%3A//www.sf.org.cn/blog/xuweidong/j2me/200509/313.html&amp;rn=815719" target="_blank" style="font-size: 1em; color: #330033; text-decoration: underline; border-bottom-width: 1px; border-bottom-color: #330033; border-bottom-style: dotted; cursor: pointer; ">转换</a>为byte数组<br />&nbsp;&nbsp; * @param file String 需要转换的文件名<br />&nbsp;&nbsp; */<br />&nbsp; private void getByte(String file)<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; byte[] myData = null;<br />&nbsp;&nbsp;&nbsp; input = getClass().getResourceAsStream(file);<br />&nbsp;&nbsp;&nbsp; try<br />&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ByteArrayOutputStream byteArray = new ByteArrayOutputStream();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int ch = 0;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((ch = input.read()) != -1)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byteArray.write(ch);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; byteArray.size(); i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myData = byteArray.toByteArray();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; myData.length; i++)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(myData[i] + ",");<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; catch (Exception e)<br />&nbsp;&nbsp;&nbsp; {}<br />&nbsp;&nbsp;&nbsp; //return myData;<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; /**<br />&nbsp;&nbsp; * 利用byte数据流生成图片<br />&nbsp;&nbsp; * @param byteFile byte[] 图片文件byte数据流<br />&nbsp;&nbsp; * @return Image 返回Image对象<br />&nbsp;&nbsp; */<br />&nbsp; private Image importImage(byte[] byteFile)<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; Image img = null;<br />&nbsp;&nbsp;&nbsp; img = Image.createImage(byteFile, 0, byteFile.length);<br />&nbsp;&nbsp;&nbsp; return img;<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; protected void paint(Graphics g)<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; /** @todo Add paint codes */<br />&nbsp;&nbsp;&nbsp; g.setColor(0xffffff);<br />&nbsp;&nbsp;&nbsp; g.fillRect(0, 0, this.getWidth(), this.getHeight());<br />&nbsp;&nbsp;&nbsp; g.drawImage(byteImg, 30, 30, g.TOP | g.LEFT);<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">&nbsp; private byte[] xiangSuImage()<br />&nbsp; {<br />&nbsp;&nbsp;&nbsp; byte[] iCaiDanImage =<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { -119, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 13, 73, 72, 68, 82, 0, 0, 0, 65, 0, 0, 0, 56,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ......//未完的数据<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;&nbsp;&nbsp; return iCaiDanImage;<br />&nbsp; }</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">}</p><p style="font-size: 14px; line-height: 24px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; ">总体来</p></div><img src ="http://www.blogjava.net/mlzry0612/aggbug/350562.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2011-05-19 08:37 <a href="http://www.blogjava.net/mlzry0612/articles/350562.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基本算法</title><link>http://www.blogjava.net/mlzry0612/articles/349968.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Tue, 10 May 2011 13:49:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/349968.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/349968.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/349968.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/349968.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/349968.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 基础排序算法通常情况下复杂算法处理小文件的开销会比基本的算法慢，这里先总结3个最基础的排序算法，即选择排序、插入排序和冒泡排序。1. 该节中通用的方法                        01            static&nbsp;void&nbsp;exch(double[] a,&nbsp;int&nbsp;i,&nbsp;int&nb...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/349968.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/349968.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2011-05-10 21:49 <a href="http://www.blogjava.net/mlzry0612/articles/349968.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Strong, Soft, Weak, Phantom Reference</title><link>http://www.blogjava.net/mlzry0612/articles/349967.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Tue, 10 May 2011 13:37:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/349967.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/349967.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/349967.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/349967.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/349967.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Java&nbsp;2&nbsp;平台引入了&nbsp;java.lang.ref&nbsp;包，其中包括的类可以让您引用对象，而不将它们留在内存中。这些类还提供了与垃圾收集器（garbage&nbsp;collector）之间有限的交互。Peter&nbsp;Haggar&nbsp;在本文中分析了&nbsp;SoftReference、WeakReference&nbsp;和&nbsp;Phan...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/349967.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/349967.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2011-05-10 21:37 <a href="http://www.blogjava.net/mlzry0612/articles/349967.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK 5.0 垃圾回收</title><link>http://www.blogjava.net/mlzry0612/articles/340830.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Wed, 15 Dec 2010 13:43:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/340830.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/340830.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/340830.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/340830.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/340830.html</trackback:ping><description><![CDATA[<div class="title" style="padding-top: 10px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; height: 56px; line-height: 56px; text-align: center; overflow-x: hidden; overflow-y: hidden; font-family: Verdana, Arial, Tahoma; font-size: 12px; ">
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; "><br />
</h2>
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; "><br />
</h2>
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; "><br />
</h2>
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; "><br />
</h2>
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; "><br />
</h2>
<h2 style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 24px; color: #336699; ">转 &nbsp;JDK5.0垃圾收集优化之--Don't Pause</h2>
</div>
<div class="info" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; height: 24px; line-height: 17px; text-align: center; overflow-x: hidden; overflow-y: hidden; color: #666666; font-family: Verdana, Arial, Tahoma; font-size: 12px; "><small style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 3px; margin-bottom: 0px; margin-left: 8px; color: #999999; font-size: 12px; ">时间:</small>2009-06-19 10:32:58<small style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 3px; margin-bottom: 0px; margin-left: 8px; color: #999999; font-size: 12px; ">来源:</small>网络&nbsp;<small style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 3px; margin-bottom: 0px; margin-left: 8px; color: #999999; font-size: 12px; ">作者:</small>未知&nbsp;<small style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 3px; margin-bottom: 0px; margin-left: 8px; color: #999999; font-size: 12px; ">点击:</small><span id="hits" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">50</span>次</div>
<div class="intro" style="padding-top: 8px; padding-right: 16px; padding-bottom: 8px; padding-left: 16px; margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 25px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #fafdfe; border-left-color: #dddddd; font-size: 14px; color: #555555; font-family: Verdana, Arial, Tahoma; ">原本想把题目更简单的定为--《不要停》的，但还是自己YY一下就算了。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Java开发Server最大的障碍，就是JDK1.4版之前的的串行垃圾收集机制会引起长时间的服务暂停，明白原理后，想想那些用JDK1.3写Server的先辈，不得不后怕</div>
<div class="content" style="padding-top: 12px; padding-right: 16px; padding-bottom: 12px; padding-left: 16px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-size: 14px; line-height: 25px; color: #333333; font-family: Verdana, Arial, Tahoma; ">
<table width="100%" style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">
    <tbody style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">
        <tr style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">
            <td style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">原本想把题目更简单的定为--《不要停》的，但还是自己YY一下就算了。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Java开发Server最大的障碍，就是JDK1.4版之前的的串行垃圾收集机制会引起长时间的服务暂停，明白原理后，想想那些用JDK1.3写Server的先辈，不得不后怕。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp;&nbsp; 好在JDK1.4已开始支持多线程并行的后台垃圾收集算法，JDK5.0则优化了默认值的设置。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">一、参考资料：</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Tuning Garbage Collection with the 5.0 Java Virtual Machine 官方指南。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            Hotspot memory management whitepaper 官方白皮书。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            Java Tuning White Paper 官方文档。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            FAQ about Garbage Collection in the Hotspot&nbsp; 官方FAQ，JVM1.4.2。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            Java HotSpot 虚拟机中的垃圾收集 JavaOne2004上的中文ppt&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            A Collection of JVM Options JVM选项的超完整收集。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            二、基本概念</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1、堆(Heap)</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">JVM管理的内存叫堆。在32Bit操作系统上有1.5G-2G的限制，而64Bit的就没有。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">JVM初始分配的内存由-Xms指定，默认是物理内存的1/64但小于1G。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">JVM最大分配的内存由-Xmx指定，默认是物理内存的1/4但小于1G。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">默认空余堆内存小于40%时，JVM就会增大堆直到-Xmx的最大限制，可以由-XX:MinHeapFreeRatio=指定。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            默认空余堆内存大于70%时，JVM会减少堆直到-Xms的最小限制，可以由-XX:MaxHeapFreeRatio=指定。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">服务器一般设置-Xms、-Xmx相等以避免在每次GC 后调整堆的大小，所以上面的两个参数没啥用。&nbsp;</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            2.基本收集算法</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">复制：将堆内分成两个相同空间，从根(ThreadLocal的对象，静态对象）开始访问每一个关联的活跃对象，将空间A的活跃对象全部复制到空间B，然后一次性回收整个空间A。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            因为只访问活跃对象，将所有活动对象复制走之后就清空整个空间，不用去访问死对象，所以遍历空间的成本较小，但需要巨大的复制成本和较多的内存。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            标记清除(mark-sweep)：收集器先从根开始访问所有活跃对象，标记为活跃对象。然后再遍历一次整个内存区域，把所有没有标记活跃的对象进行回收处理。该算法遍历整个空间的成本较大暂停时间随空间大小线性增大，而且整理后堆里的碎片很多。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            标记整理(mark-sweep-compact)：综合了上述两者的做法和优点，先标记活跃对象，然后将其合并成较大的内存块。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 可见，没有免费的午餐，无论采用复制还是标记清除算法，自动的东西都要付出很大的性能代价。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.分代</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 分代是Java垃圾收集的一大亮点，根据对象的生命周期长短，把堆分为3个代：Young，Old和Permanent，根据不同代的特点采用不同的收集算法，扬长避短也。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Young(Nursery)，年轻代。研究表明大部分对象都是朝生暮死，随生随灭的。因此所有收集器都为年轻代选择了复制算法。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 复制算法优点是只访问活跃对象，缺点是复制成本高。因为年轻代只有少量的对象能熬到垃圾收集，因此只需少量的复制成本。而且复制收集器只访问活跃对象，对那些占了最大比率的死对象视而不见，充分发挥了它遍历空间成本低的优点。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; Young的默认值为4M，随堆内存增大，约为1/15，JVM会根据情况动态管理其大小变化。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; -XX:NewRatio= 参数可以设置Young与Old的大小比例，-server时默认为1:2，但实际上young启动时远低于这个比率？如果信不过JVM，也可以用-Xmn硬性规定其大小，有文档推荐设为Heap总大小的1/4。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; Young的大小非常非常重要，见&#8220;后面暂停时间优先收集器&#8221;的论述。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; Young里面又分为3个区域，一个Eden，所有新建对象都会存在于该区，两个Survivor区，用来实施复制算法。每次复制就是将Eden和第一块Survior的活对象复制到第2块，然后清空Eden与第一块Survior。Eden与Survivor的比例由-XX:SurvivorRatio=设置，默认为32。Survivio大了会浪费，小了的话，会使一些年轻对象潜逃到老人区，引起老人区的不安，但这个参数对性能并不重要。&nbsp;</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Old(Tenured)，年老代。年轻代的对象如果能够挺过数次收集，就会进入老人区。老人区使用标记整理算法。因为老人区的对象都没那么容易死的，采用复制算法就要反复的复制对象，很不合算，只好采用标记清理算法，但标记清理算法其实也不轻松，每次都要遍历区域内所有对象，所以还是没有免费的午餐啊。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">-XX:MaxTenuringThreshold=设置熬过年轻代多少次收集后移入老人区，CMS中默认为0，熬过第一次GC就转入，可以用-XX:+PrintTenuringDistribution查看。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Permanent，持久代。装载Class信息等基础数据，默认64M，如果是类很多很多的服务程序，需要加大其设置-XX:MaxPermSize=，否则它满了之后会引起fullgc()或Out of Memory。 注意Spring，Hibernate这类喜欢AOP动态生成类的框架需要更多的持久代内存。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">4.minor/major collection</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 每个代满了之后都会促发collection，（另外Concurrent Low Pause Collector默认在老人区68%的时候促发)。GC用较高的频率对young进行扫描和回收，这种叫做minor collection。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            而因为成本关系对Old的检查回收频率要低很多，同时对Young和Old的收集称为major collection。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; System.gc()会引发major collection，使用-XX:+DisableExplicitGC禁止它，或设为CMS并发-XX:+ExplicitGCInvokesConcurrent。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">5.小结</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Young -- minor collection -- 复制算法</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Old(Tenured) -- major colletion -- 标记清除/标记整理算法</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            三、收集器</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1.古老的串行收集器(Serial Collector)</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 使用 -XX:+UseSerialGC，策略为年轻代串行复制，年老代串行标记整理。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">2.吞吐量优先的并行收集器(Throughput Collector)</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 使用 -XX:+UseParallelGC ，也是JDK5 -server的默认值。策略为：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 1.年轻代暂停应用程序，多个垃圾收集线程并行的复制收集，线程数默认为CPU个数，CPU很多时，可用&#8211;XX:ParallelGCThreads=减少线程数。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 2.年老代暂停应用程序，与串行收集器一样，单垃圾收集线程标记整理。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 所以需要2+的CPU时才会优于串行收集器，适用于后台处理，科学计算。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 可以使用-XX:MaxGCPauseMillis= 和 -XX:GCTimeRatio 来调整GC的时间。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.暂停时间优先的并发收集器(Concurrent Low Pause Collector-CMS)</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 前面说了这么多，都是为了这节做铺垫......</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 使用-XX:+UseConcMarkSweepGC，策略为：<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 1.年轻代同样是暂停应用程序，多个垃圾收集线程并行的复制收集。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp; 2.年老代则只有两次短暂停，其他时间应用程序与收集线程并发的清除。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.1 年老代详述</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 并行(Parallel)与并发(Concurrent)仅一字之差，并行指多条垃圾收集线程并行，并发指用户线程与垃圾收集线程并发，程序在继续运行，而垃圾收集程序运行于另一个个CPU上。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 并发收集一开始会很短暂的停止一次所有线程来开始初始标记根对象，然后标记线程与应用线程一起并发运行，最后又很短的暂停一次，多线程并行的重新标记之前可能因为并发而漏掉的对象，然后就开始与应用程序并发的清除过程。可见，最长的两个遍历过程都是与应用程序并发执行的，比以前的串行算法改进太多太多了！！！</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 串行标记清除是等年老代满了再开始收集的，而并发收集因为要与应用程序一起运行，如果满了才收集，应用程序就无内存可用，所以系统默认68%满的时候就开始收集。内存已设得较大，吃内存又没有这么快的时候，可以用-XX:CMSInitiatingOccupancyFraction=恰当增大该比率。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.2 年轻代详述</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp; 可惜对年轻代的复制收集，依然必须停止所有应用程序线程，原理如此，只能靠多CPU，多收集线程并发来提高收集速度，但除非你的Server独占整台服务器，否则如果服务器上本身还有很多其他线程时，切换起来速度就..... 所以，搞到最后，暂停时间的瓶颈就落在了年轻代的复制算法上。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp;&nbsp; 因此Young的大小设置挺重要的，大点就不用频繁GC，而且增大GC的间隔后，可以让多点对象自己死掉而不用复制了。但Young增大时，GC造成的停顿时间攀升得非常恐怖，比如在我的机器上，默认8M的Young，只需要几毫秒的时间，64M就升到90毫秒，而升到256M时，就要到300毫秒了，峰值还会攀到恐怖的800ms。谁叫复制算法，要等Young满了才开始收集，开始收集就要停止所有线程呢。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.3 持久代</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">可设置-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled，使CMS收集持久代的类，而不是fullgc，netbeans5.5 performance文档的推荐。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">4.增量(train算法)收集器(Incremental Collector)</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">已停止维护，&#8211;Xincgc选项默认转为并发收集器。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">四、暂停时间显示</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;加入下列参数 (请将PrintGC和Details中间的空格去掉，CSDN很怪的认为是禁止字句）&nbsp;</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">-verbose:gc -XX:+PrintGC Details&nbsp; -XX:+PrintGCTimeStamps</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">会程序运行过程中将显示如下输出</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;9.211: [GC 9.211: [ParNew: 7994K-&gt;0K(8128K), 0.0123935 secs] 427172K-&gt;419977K(524224K), 0.0125728 secs]</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;显示在程序运行的9.211秒发生了Minor的垃圾收集，前一段数据针对新生区，从7994k整理为0k，新生区总大小为8128k，程序暂停了12ms，而后一段数据针对整个堆。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">对于年老代的收集，暂停发生在下面两个阶段，CMS-remark的中断是17毫秒：</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">[GC [1 CMS-initial-mark: 80168K(196608K)] 81144K(261184K), 0.0059036 secs]&nbsp;</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">[1 CMS-remark: 80168K(196608K)] 82493K(261184K),0.0168943 secs]</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">再加两个参数 -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime对暂停时间看得更清晰。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">五、真正不停的BEA JRockit 与Sun RTS2.0</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;&nbsp; Bea的JRockit 5.0 R27 的特色之一是动态决定的垃圾收集策略，用户可以决定自己关心的是吞吐量，暂停时间还是确定的暂停时间，再由JVM在运行时动态决定、改变改变垃圾收集策略。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp;&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;&nbsp; 它的Deterministic GC的选项是-Xgcprio: deterministic，号称可以把暂停可以控制在10-30毫秒，非常的牛，一句Deterministic道尽了RealTime的真谛。 不过细看一下文档，30ms的测试环境是1 GB heap 和 平均&nbsp; 30% 的活跃对象(也就是300M)活动对象，2 个 Xeon 3.6 GHz&nbsp; 4G内存 ，或者是4 个Xeon 2.0 GHz，8G内存。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp; 最可惜JRockt的license很奇怪，虽然平时使用免费，但这个30ms的选项就需要购买整个Weblogic Real Time Server的license。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp; 其他免费选项，有：</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">-Xgcprio:pausetime -Xpausetarget=210ms&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp; 因为免费，所以最低只能设置到200ms pause target。 200ms是Sun认为Real-Time的分界线。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            -Xgc:gencon<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            普通的并发做法，效率也不错。&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp; JavaOne2007上有Sun的 Java Real-Time System 2.0 的介绍，RTS2.0基于JDK1.5，在Real-Time&nbsp; Garbage Collctor上又有改进，但还在beta版状态，只供给OEM，更怪。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">六、JDK 6.0的改进</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">因为JDK5.0在Young较大时的表现还是不够让人满意，又继续看JDK6.0的改进，结果稍稍失望，不涉及我最头痛的年轻代复制收集改良。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1.年老代的标识-清除收集，并行执行标识<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp; JDK5.0只开了一条收集进程与应用线程并发标识，而6.0可以开多条收集线程来做标识，缩短标识老人区所有活动对象的时间。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">2.加大了Young区的默认大小<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            默认大小从4M加到16M，从堆内存的1/15增加到1/7</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">3.System.gc()可以与应用程序并发执行<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            使用-XX:+ExplicitGCInvokesConcurrent 设置</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">七、小结</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1. JDK5.0/6.0</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">对于服务器应用，我们使用Concurrent Low Pause Collector，对年轻代，暂停时多线程并行复制收集；对年老代，收集器与应用程序并行标记--整理收集，以达到尽量短的垃圾收集时间。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">本着没有深刻测试前不要胡乱优化的宗旨，命令行属性只需简单写为：</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">-server -Xms&lt;heapsize&gt;M -Xmx&lt;heapsize&gt;M -XX:+UseConcMarkSweepGC&nbsp; -XX:+PrintGC Details&nbsp; -XX:+PrintGCTimeStamps&nbsp;<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            然后要根据应用的情况，在测试软件辅助可以下看看有没有JVM的默认值和自动管理做的不够的地方可以调整，如-xmn 设Young的大小，-XX:MaxPermSize设持久代大小等。</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">2. JRockit 6.0 R27.2</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">但因为JDK5的测试结果实在不能满意，后来又尝试了JRockit，总体效果要好些。<br style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; " />
            &nbsp;JRockit的特点是动态垃圾收集器是根据用户关心的特征动态决定收集算法的，参数如下</p>
            <p style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">&nbsp;-Xms&lt;heapsize&gt;M -Xmx&lt;heapsize&gt;M -Xgcprio:pausetime -Xpausetarget=200ms -XgcReport -XgcPause -Xverbose:memory</p>
            </td>
        </tr>
    </tbody>
</table>
</div>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/340830.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-12-15 21:43 <a href="http://www.blogjava.net/mlzry0612/articles/340830.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Thread local for MUL threads</title><link>http://www.blogjava.net/mlzry0612/articles/340177.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Thu, 09 Dec 2010 11:27:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/340177.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/340177.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/340177.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/340177.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/340177.html</trackback:ping><description><![CDATA[<p font-size:=""  10pt;="" style="font-size: 10pt; font-family: 宋体, arial, serif; line-height: 24px; ">&nbsp;&nbsp; &nbsp;ThreadLocal的核心思想很简单：为每个独立的线程提供一个变量的副本。</p>
<p font-size:=""  10pt;="" style="font-size: 10pt; font-family: 宋体, arial, serif; line-height: 24px; ">&nbsp;&nbsp;&nbsp; ThreadLocal则使用了&#8220;拷贝副本&#8221;的方式，人人有份，你用你的，我用我的，大家互不影响，是&#8220;以空间换时间&#8221;。每个线程修改变量时，实际上修改的是变量的副本，不怕影响到其它线程。</p>
<p font-size:=""  10pt;="" style="font-size: 10pt; font-family: 宋体, arial, serif; line-height: 24px; ">&nbsp;&nbsp;&nbsp; 为了加深对ThreadLocal的理解，下面我使用一个例子来演示ThreadLocal如何隔离线程间的变量访问和修改：<br />
<br />
【1】SerialNum类</p>
<div align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">
<div twffan="done" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; font-size: 13px; border-left-color: #cccccc; width: 597px; word-break: break-all; background-color: #eeeeee; ">
<div align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">package&nbsp;example.thread.threadLocal;<br />
<br />
public&nbsp;class&nbsp;SerialNum&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;int&nbsp;nextSerialNum&nbsp;=&nbsp;1;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;ThreadLocal&nbsp;serialNum&nbsp;=&nbsp;new&nbsp;ThreadLocal()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;synchronized&nbsp;Object&nbsp;initialValue()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;new&nbsp;Integer(nextSerialNum++);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;};<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;int&nbsp;get()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;((Integer)&nbsp;(serialNum.get())).intValue();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;@SuppressWarnings("unchecked")<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;set(Integer&nbsp;newSerial){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serialNum.set(newSerial);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</div>
</div>
</div>
<span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">【2】GetSerialNumThread</span>
<div align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">
<div twffan="done" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; font-size: 13px; border-left-color: #cccccc; width: 597px; word-break: break-all; background-color: #eeeeee; ">
<div align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">package&nbsp;example.thread.threadLocal;<br />
<br />
public&nbsp;class&nbsp;GetSerialNumThread&nbsp;implements&nbsp;Runnable&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String&nbsp;args[])&nbsp;{<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetSerialNumThread&nbsp;serialNumGetter&nbsp;=&nbsp;new&nbsp;GetSerialNumThread();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread&nbsp;t1&nbsp;=&nbsp;new&nbsp;Thread(serialNumGetter,&nbsp;"Thread&nbsp;A");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread&nbsp;t2&nbsp;=&nbsp;new&nbsp;Thread(serialNumGetter,&nbsp;"Thread&nbsp;B");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1.start();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t1.join();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(InterruptedException&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t2.start();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;run()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;mySerialNum&nbsp;=&nbsp;getSerialNum();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("线程&nbsp;"&nbsp;+&nbsp;Thread.currentThread().getName()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;"&nbsp;获取到的序列号是"&nbsp;+&nbsp;mySerialNum);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("线程&nbsp;"&nbsp;+&nbsp;Thread.currentThread().getName()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;"&nbsp;修改了序列号为"&nbsp;+&nbsp;(mySerialNum&nbsp;*&nbsp;3));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setSerialNum(mySerialNum&nbsp;*&nbsp;3);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("线程&nbsp;"&nbsp;+&nbsp;Thread.currentThread().getName()<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+&nbsp;"&nbsp;再次获得的序列号是"&nbsp;+&nbsp;getSerialNum());<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;int&nbsp;getSerialNum()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;SerialNum.get();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;void&nbsp;setSerialNum(int&nbsp;newSerialNum)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SerialNum.set(new&nbsp;Integer(newSerialNum));<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</div>
</div>
</div>
<span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">运行的结果如下：</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span>
<div align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">
<div twffan="done" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; font-size: 13px; border-left-color: #cccccc; width: 597px; word-break: break-all; background-color: #eeeeee; ">
<div align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">线程&nbsp;Thread&nbsp;A&nbsp;获取到的序列号是1<br />
线程&nbsp;Thread&nbsp;A&nbsp;修改了序列号为3<br />
线程&nbsp;Thread&nbsp;A&nbsp;再次获得的序列号是3<br />
线程&nbsp;Thread&nbsp;B&nbsp;获取到的序列号是2<br />
线程&nbsp;Thread&nbsp;B&nbsp;修改了序列号为6<br />
线程&nbsp;Thread&nbsp;B&nbsp;再次获得的序列号是6</div>
</div>
</div>
<span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">&nbsp;&nbsp;&nbsp; 可见第一个线程在调用SerialNum.set(int)方法修改static变量时，其实修改的是它自己的副本，而不是修改本地变量，第二个线程在初始化的时候拿到的序列号是2而不是7。</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">&nbsp;&nbsp;&nbsp; 为什么会这样呢？明明serialNum是静态变量啊？其实我们只需要看看ThreadLocal的内部构造就知道了：</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><strong>A. ThreadLocal的get()方法：<br />
</strong></span>
<div align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">
<div twffan="done" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; font-size: 13px; border-left-color: #cccccc; width: 597px; word-break: break-all; background-color: #eeeeee; ">
<div align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;/**<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Returns&nbsp;the&nbsp;value&nbsp;in&nbsp;the&nbsp;current&nbsp;thread's&nbsp;copy&nbsp;of&nbsp;this&nbsp;thread-local<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;variable.&nbsp;&nbsp;Creates&nbsp;and&nbsp;initializes&nbsp;the&nbsp;copy&nbsp;if&nbsp;this&nbsp;is&nbsp;the&nbsp;first&nbsp;time<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;the&nbsp;thread&nbsp;has&nbsp;called&nbsp;this&nbsp;method.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;the&nbsp;current&nbsp;thread's&nbsp;value&nbsp;of&nbsp;this&nbsp;thread-local<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;T&nbsp;get()&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread&nbsp;t&nbsp;=&nbsp;Thread.currentThread();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadLocalMap&nbsp;map&nbsp;=&nbsp;getMap(t);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(map&nbsp;!=&nbsp;null)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;(T)map.get(this);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Maps&nbsp;are&nbsp;constructed&nbsp;lazily.&nbsp;&nbsp;if&nbsp;the&nbsp;map&nbsp;for&nbsp;this&nbsp;thread<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;doesn't&nbsp;exist,&nbsp;create&nbsp;it,&nbsp;with&nbsp;this&nbsp;ThreadLocal&nbsp;and&nbsp;its<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;initial&nbsp;value&nbsp;as&nbsp;its&nbsp;only&nbsp;entry.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;value&nbsp;=&nbsp;initialValue();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createMap(t,&nbsp;value);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;value;<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</div>
</div>
</div>
<span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><strong>B. ThreadLocal的set()方法：</strong></span>
<div align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">
<div twffan="done" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 4px; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; font-size: 13px; border-left-color: #cccccc; width: 597px; word-break: break-all; background-color: #eeeeee; ">
<div align="left" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">/**<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Sets&nbsp;the&nbsp;current&nbsp;thread's&nbsp;copy&nbsp;of&nbsp;this&nbsp;thread-local&nbsp;variable<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;to&nbsp;the&nbsp;specified&nbsp;value.&nbsp;&nbsp;Many&nbsp;applications&nbsp;will&nbsp;have&nbsp;no&nbsp;need&nbsp;for<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;this&nbsp;functionality,&nbsp;relying&nbsp;solely&nbsp;on&nbsp;the&nbsp;{@link&nbsp;#initialValue}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;method&nbsp;to&nbsp;set&nbsp;the&nbsp;values&nbsp;of&nbsp;thread-locals.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;value&nbsp;the&nbsp;value&nbsp;to&nbsp;be&nbsp;stored&nbsp;in&nbsp;the&nbsp;current&nbsp;threads'&nbsp;copy&nbsp;of<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this&nbsp;thread-local.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;set(T&nbsp;value)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread&nbsp;t&nbsp;=&nbsp;Thread.currentThread();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ThreadLocalMap&nbsp;map&nbsp;=&nbsp;getMap(t);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(map&nbsp;!=&nbsp;null)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map.set(this,&nbsp;value);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;createMap(t,&nbsp;value);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</div>
</div>
</div>
<span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; "><br />
</span><span  style="font-family: 宋体, arial, serif; line-height: 24px; font-size: 11.6667px; ">&nbsp;&nbsp;&nbsp; 可以看到ThreadLocal在内部维护了一个Map，将变量的值和线程绑定起来，get/set方法都是对该线程对应的value进行操作，所以不会影响到其它线程。</span>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/340177.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-12-09 19:27 <a href="http://www.blogjava.net/mlzry0612/articles/340177.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>解压缩jar文件</title><link>http://www.blogjava.net/mlzry0612/articles/338457.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Fri, 19 Nov 2010 02:57:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/338457.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/338457.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/338457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/338457.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/338457.html</trackback:ping><description><![CDATA[<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">如果已存在以压缩文件a.jar,现在想把一个a.class文件或com/my/b.calss包含文件夹的文件添加到压缩包中；</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">1.创建临时文件</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">2..先解压目标压缩Jar文件 到临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">3.拷贝源文件到临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">4.再压缩临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">5.再删除临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">方法可能有些笨，但是我找了好久也没找到现成API实现这个功能。所以和大家分享，共写了三个类</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">第一个类：</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">package com.jar;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">import java.io.File;<br style="line-height: normal; " />
import java.io.FileInputStream;<br style="line-height: normal; " />
import java.io.FileOutputStream;<br style="line-height: normal; " />
import java.io.IOException;<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
* 文件夹或文件拷贝到指定目录<br style="line-height: normal; " />
* 文件夹的删除<br style="line-height: normal; " />
* @author zhang<br style="line-height: normal; " />
*<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public class FolderCopy {</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 测试主程序<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public static void main(String args[]) throws IOException {<br style="line-height: normal; " />
&nbsp;&nbsp; String url1 = "D:/wtk/SonyEricsson/JavaME_SDK_CLDC/README.html";<br style="line-height: normal; " />
&nbsp;&nbsp; String url2 = "c:/temp/";<br style="line-height: normal; " />
&nbsp;&nbsp; copyDirectiory(url1, url2);<br style="line-height: normal; " />
}<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
* 文件夹拷贝，将源文件夹下的所有文件及其子文件夹（文件）拷贝到目标文件夹（文件）下<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
* @param sourceFile<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 源文件夹或文件<br style="line-height: normal; " />
* @param desFile<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 目标文件夹<br style="line-height: normal; " />
* @return<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public static boolean copyDirectiory(String sourceFile, String desFile)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; throws IOException {<br style="line-height: normal; " />
&nbsp;&nbsp; File des = new File(desFile);<br style="line-height: normal; " />
&nbsp;&nbsp; if (!des.exists())<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; des.mkdirs();// 不存在目标文件夹就创建<br style="line-height: normal; " />
&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; File source = new File(sourceFile);<br style="line-height: normal; " />
&nbsp;&nbsp; if (!source.exists()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; System.out.println(source.getAbsolutePath()<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + "========源文件不存在！=======");<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; FileInputStream input = null;<br style="line-height: normal; " />
&nbsp;&nbsp; FileOutputStream output = null;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (source.isFile()) { // 如果是文件 则读源文件 写入目标文件<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; input = new FileInputStream(source);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; File f = new File(desFile + "/"<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + source.getName());<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; output = new FileOutputStream(f);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; byte[] b = new byte[1024 * 5];<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; int len;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; while ((len = input.read(b)) != -1) { // 读文件<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.write(b, 0, len); // 向目标文件写文件<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; input.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; output.flush();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; output.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; } else if (source.isDirectory()) { // 如果是文件夹 递归读取子文件或文件夹<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; File[] file = source.listFiles();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; file.length; i++) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(file[i].isDirectory())<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; copyDirectiory(sourceFile + "/" + file[i].getName(),<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; desFile + "/" + file[i].getName());<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else{<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; copyDirectiory(sourceFile + "/" + file[i].getName(),<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; desFile + "/");<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return true;<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (Exception e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal; " />
&nbsp;&nbsp; } finally {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (input != null)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; input.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (output != null)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; output.close();<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
* 刪除文件或文件夾<br style="line-height: normal; " />
* @param file<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public static void deleteFile(File file){&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; if (file.exists()){&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(file.isFile()){&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.delete();&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if(file.isDirectory()){&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File files[] = file.listFiles();&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(int i=0;i&lt;files.length;i++){&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; deleteFile(files[i]);&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.delete();&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }else{&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("所删除的文件不存在！"+'\n');&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; "><br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">第二个类BatchJar ：</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">package com.jar;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">import java.io.File;<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
* 批处理：在指定文件夹中的所有Jar文件中添加文件或文件夹<br style="line-height: normal; " />
* @author zhang<br style="line-height: normal; " />
*<br style="line-height: normal; " />
*/</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">public class BatchJar {</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* @param args<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public static void main(String[] args) {<br style="line-height: normal; " />
&nbsp;&nbsp; // TODO Auto-generated method stub<br style="line-height: normal; " />
&nbsp;&nbsp; BatchJar jar = new BatchJar();<br style="line-height: normal; " />
&nbsp;&nbsp; jar.batchJar("D:/temp/疯狂农场", "jar", "D:/temp/ashinanren.jar");<br style="line-height: normal; " />
}<br style="line-height: normal; " />
public void batchJar(String dir, String fileType, String fileName){<br style="line-height: normal; " />
&nbsp;&nbsp; System.out.println(fileName+" 即将被添加到"+dir+"中的"+fileType+"文件中..");<br style="line-height: normal; " />
&nbsp;&nbsp; batchRar(dir,fileType,fileName);<br style="line-height: normal; " />
&nbsp;&nbsp; System.out.println("添加完成！");<br style="line-height: normal; " />
}<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
* @param dir<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 需要遍历的文件夹<br style="line-height: normal; " />
* @param fileType<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 压缩文件类型 rar jar zip<br style="line-height: normal; " />
* @param fileName<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要添加的文件或文件夹<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 遍历文件夹 拼写批处理命令<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
JARDemo jar = new JARDemo();<br style="line-height: normal; " />
public void batchRar(String dir, String fileType, String fileName) {</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; File file = new File(dir);<br style="line-height: normal; " />
&nbsp;&nbsp; if (file.exists()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (file.isFile()) { //<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; if (isType(file, fileType)) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; jar.fileAddToJar(fileName, file.getAbsolutePath());&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; } else if (file.isDirectory()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; File files[] = file.listFiles();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; files.length; i++) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; batchRar(files[i].getAbsolutePath(), fileType, fileName);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; // file.delete();<br style="line-height: normal; " />
&nbsp;&nbsp; } else {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; System.out.println("所删除的文件不存在！" + '\n');<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">}<br style="line-height: normal; " />
/**<br style="line-height: normal; " />
* 判断是否为指定文件类型<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
private boolean isType(File file, String fileType) {<br style="line-height: normal; " />
&nbsp;&nbsp; if (file == null) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; String fileName = file.getAbsolutePath();<br style="line-height: normal; " />
&nbsp;&nbsp; String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);// 后缀<br style="line-height: normal; " />
&nbsp;&nbsp; // System.out.println("====后缀："+suffix);<br style="line-height: normal; " />
&nbsp;&nbsp; if (fileType.equals(suffix)) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return true;<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; return false;<br style="line-height: normal; " />
}<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">第三个类JARDemo ：<br style="line-height: normal; " />
package com.jar;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">import java.io.File;<br style="line-height: normal; " />
import java.io.FileInputStream;<br style="line-height: normal; " />
import java.io.FileNotFoundException;<br style="line-height: normal; " />
import java.io.FileOutputStream;<br style="line-height: normal; " />
import java.io.IOException;<br style="line-height: normal; " />
import java.io.InputStream;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">import java.util.Enumeration;<br style="line-height: normal; " />
import java.util.jar.JarEntry;<br style="line-height: normal; " />
import java.util.jar.JarFile;<br style="line-height: normal; " />
import java.util.jar.JarOutputStream;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 压缩指定文件夹<br style="line-height: normal; " />
* 解压指定压缩文件<br style="line-height: normal; " />
* 把指定源文件或文件夹添加到指定压缩包中<br style="line-height: normal; " />
* @author zhang<br style="line-height: normal; " />
*<br style="line-height: normal; " />
*/</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">public class JARDemo {<br style="line-height: normal; " />
public static void main(String[] args) {<br style="line-height: normal; " />
&nbsp;&nbsp; JARDemo t = new JARDemo();</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; t.fileAddToJar("d:/com", "d:/temp/shinanren.jar");<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 把指定源文件添加到指定压缩包中<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
* @param sourceFileName<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 要压缩到指定目标压缩文件的源文件或文件夹<br style="line-height: normal; " />
* @param destFileName<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 目标压缩文件<br style="line-height: normal; " />
* @throws IOException<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public void fileAddToJar(String sourceFileName, String jarName) {<br style="line-height: normal; " />
//&nbsp;&nbsp; System.out.println("**=="+sourceFileName);<br style="line-height: normal; " />
&nbsp;&nbsp; // 先解压到临时文件夹，存在先刪除 不存在直接創建<br style="line-height: normal; " />
&nbsp;&nbsp; String tempFileName = "c:/temp";<br style="line-height: normal; " />
&nbsp;&nbsp; File tempDir = new File(tempFileName);<br style="line-height: normal; " />
&nbsp;&nbsp; try {</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; if (tempDir.exists()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; FolderCopy.deleteFile(tempDir);// 刪除文件夾<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; } else {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; tempDir.mkdirs();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (Exception e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; this.unJar(jarName, tempFileName);// 先解压目标压缩Jar文件 到临时文件夹<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; File f = new File(sourceFileName);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if(f.isDirectory()){//如果是文件夹<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; FolderCopy.copyDirectiory(sourceFileName, tempFileName+"/"+new File(sourceFileName).getName());// 拷贝源文件到临时文件夹<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if(f.isFile())<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; FolderCopy.copyDirectiory(sourceFileName, tempFileName);// 拷贝源文件到临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; this.jar(tempFileName, jarName);// 再压缩临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; FolderCopy.deleteFile(tempDir); //再删除临时文件夹</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; } catch (IOException e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; // TODO Auto-generated catch block<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 压缩指定文件夹<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
* @param souceFileName<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 源文件<br style="line-height: normal; " />
* @param destFileName<br style="line-height: normal; " />
*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 目标文件<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
private void jar(String souceFileName, String destFileName) {<br style="line-height: normal; " />
&nbsp;&nbsp; destName = destFileName;<br style="line-height: normal; " />
&nbsp;&nbsp; File file = new File(souceFileName);<br style="line-height: normal; " />
&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; jar(file, destFileName);<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (Exception e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">private void jar(File souceFile, String destFileName) throws IOException {<br style="line-height: normal; " />
&nbsp;&nbsp; FileOutputStream fileOut = null;<br style="line-height: normal; " />
&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; fileOut = new FileOutputStream(destFileName);<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (FileNotFoundException e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; JarOutputStream out = new JarOutputStream(fileOut);<br style="line-height: normal; " />
&nbsp;&nbsp; jar(souceFile, out, "");<br style="line-height: normal; " />
&nbsp;&nbsp; out.close();<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">String destName = "";</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">private void jar(File souceFile, JarOutputStream out, String base)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; throws IOException {</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; if (souceFile.isDirectory()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; File[] files = souceFile.listFiles();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; out.putNextEntry(new JarEntry(base + "/"));<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; base = base.length() == 0 ? "" : base + "/";<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; for (File file : files) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; jar(file, out, base + file.getName());<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; } else {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (base.length() &gt; 0) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; out.putNextEntry(new JarEntry(base));<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; } else {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; out.putNextEntry(new JarEntry(souceFile.getName()));<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; FileInputStream in = new FileInputStream(souceFile);</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; int b;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; byte[] by = new byte[1024];<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; while ((b = in.read(by)) != -1) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; out.write(by, 0, b);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; in.close();<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 解压缩文件<br style="line-height: normal; " />
*&nbsp;<br style="line-height: normal; " />
* @param zipFilename<br style="line-height: normal; " />
* @param outputDirectory<br style="line-height: normal; " />
* @throws IOException<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public synchronized void unJar(String jarFilename, String outputDirectory)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; throws IOException {<br style="line-height: normal; " />
&nbsp;&nbsp; File outFile = new File(outputDirectory);<br style="line-height: normal; " />
&nbsp;&nbsp; if (!outFile.exists()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; outFile.mkdirs();<br style="line-height: normal; " />
&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp; JarFile jarFile = new JarFile(jarFilename);<br style="line-height: normal; " />
&nbsp;&nbsp; Enumeration en = jarFile.entries();<br style="line-height: normal; " />
&nbsp;&nbsp; JarEntry jarEntry = null;<br style="line-height: normal; " />
&nbsp;&nbsp; while (en.hasMoreElements()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; jarEntry = (JarEntry) en.nextElement();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (jarEntry.isDirectory()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; // mkdir directory<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; String dirName = jarEntry.getName();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; dirName = dirName.substring(0, dirName.length() - 1);</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; File f = new File(outFile.getPath() + File.separator + dirName);<br style="line-height: normal; " />
//&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; } else {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; // unjar file<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; File f = new File(outFile.getPath() + File.separator<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; + jarEntry.getName());<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; this.createFile(f);</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; InputStream in = jarFile.getInputStream(jarEntry);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; FileOutputStream out = new FileOutputStream(f);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] by = new byte[1024];<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ((c = in.read(by)) != -1) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.write(by, 0, c);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // out.flush();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; } catch (IOException e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; } finally {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 创建文件 包含不存在的文件夹<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public boolean createFile(File file) {<br style="line-height: normal; " />
//&nbsp;&nbsp; System.out.println("=="+file.getAbsolutePath());<br style="line-height: normal; " />
&nbsp;&nbsp; if (file == null)<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal; " />
&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; if (file.isDirectory()&amp;&amp;!file.exists()) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; file.mkdirs();// 创建目录<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; } else {&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; File fileP = new File(file.getParent());<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; if(!fileP.exists()){<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fileP.mkdirs();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; file.createNewFile();//先确保创建文件夹目录后创建文件<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return true;<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (Exception e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; return false;<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">/**<br style="line-height: normal; " />
* 把指定文件打包 只能是文件不能文件夹<br style="line-height: normal; " />
*/<br style="line-height: normal; " />
public void fileJar() {<br style="line-height: normal; " />
&nbsp;&nbsp; String[] filenames = new String[] { "d:/d.jar", "d:/a.txt" };<br style="line-height: normal; " />
&nbsp;&nbsp; byte[] buf = new byte[1024];<br style="line-height: normal; " />
&nbsp;&nbsp; try {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; // Create the Jar file<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; String outFilename = "d:/d.jar";<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; JarOutputStream out = new JarOutputStream(new FileOutputStream(<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; outFilename));</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; filenames.length; i++) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; FileInputStream in = new FileInputStream(filenames[i]);</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; // Add Jar entry to output stream.<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("==" + filenames[i]);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; out.putNextEntry(new JarEntry(filenames[i]));</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; // Transfer bytes from the file to the Jar file<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; int len;<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; while ((len = in.read(buf)) &gt; 0) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.write(buf, 0, len);<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; // Complete the entry</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp;&nbsp; out.closeEntry();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp;&nbsp; in.close();<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; }</p>
<p style="line-height: normal; color: #333333; font-family: 宋体; font-size: 10px; ">&nbsp;&nbsp;&nbsp; // Complete the Jar file<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; out.close();<br style="line-height: normal; " />
&nbsp;&nbsp; } catch (Exception e) {<br style="line-height: normal; " />
&nbsp;&nbsp;&nbsp; e.printStackTrace();<br style="line-height: normal; " />
&nbsp;&nbsp; }<br style="line-height: normal; " />
}<br style="line-height: normal; " />
}</p>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/338457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-11-19 10:57 <a href="http://www.blogjava.net/mlzry0612/articles/338457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java的垃圾回收对程序的影响</title><link>http://www.blogjava.net/mlzry0612/articles/336381.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Thu, 28 Oct 2010 06:45:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/336381.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/336381.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/336381.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/336381.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/336381.html</trackback:ping><description><![CDATA[<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; ">J<a style="color: #003366; text-decoration: none; ">ava编程是内存受限模式下的编程， 内存对于 Java 程序来说，是非常有限的资源，所以了解Garbage Collection的机制很重要。<br />
我们只要加入java的启动参数 -verbose:gc，就可以用查看垃圾回收的情况。</a></p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
下面是有关参数的几个说明<br />
-verbose:gc在虚拟机发生内存回收时在输出设备显示信息，该参数用来监视虚拟机内存回收的情况。<br />
-Xnoclassgc<br />
关闭虚拟机对class的垃圾回收功能。<br />
-Xincgc<br />
启动增量垃圾收集器，缺省是关闭的。增量垃圾收集器能减少偶然发生的长时间的垃圾回收造成的暂停时间。但增量垃圾收集器和应用程序并发执行，因此会占用部分CPU在应用程序上的功能。<br />
-Xloggc:&lt;file&gt;<br />
将虚拟机每次垃圾回收的信息写到日志文件中，文件名由file指定，文件格式是平文件，内容和-verbose:gc输出内容相同。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
JVM管理的内存，通常叫做堆(heap)，如果 JVM 不能在 java 堆中获得更多内存来分配更多 java 对象，将会抛出 java 内存不足错误。如果 java 堆充满了活动对象，并且 JVM 无法再扩展 java 堆，那么它将不能分配更多 java 对象。<br />
在这种情况下，JVM就出现 java.lang.OutOfMemoryError 。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
JVM启动后，保留一段地址空间，这个空间的大小由-Xmx指定。这块空间的大小就是heap可能的最大值，但一开始不一定全都分配了物理内存，初始分配的heap大小由-Xms指定，一般的jdk都有最小值，如果-Xms小于-Xmx，需要的时候，向OS申请<br />
当JVM进行GC的时候，是要消耗CPU资源和需要一定时间的，这会影响到程序的正常运行，因此需要尽可能减少GC消耗的时间。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
Java程序运行过程中，对象的生命周期有长有短，所以Java Heap分为3个区，Young，Old和Permanent。当Young被填满时，GC会将对象移到Old区。Permanent是一个永久区域，分配给JVM，可以通过 命令行参数 -XX:MaxPermSize=64m 来设置，permanent generation添满后导致Full GC。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
young generation由一块Eden和两块Survivor Space构成。新创建的对象的内存都分配自eden。一块Survivor Space是空闲的，用作copying collection的目标空间。Minor collection的过程就是将eden和在用survivor space中的活对象copy到空闲survivor space中。对象在survivor spaces 里经历了数次copied后，就会被移到tenured generation或tenured掉。<br />
GC用较高的频率对young generation进行扫描和回收，这种叫做minor collection，而对old generation的检查回收频率要低很多，称为major collection。这样就不需要每次GC都将内存中所有对象都检查一遍。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
老版本的JVM是分代垃圾收集（generational garbage collection），有两种收集方式：<br />
第一个是scavenge，将所有活的对象搬到另外一块内存后，整块内存回收。这种方法有效率，但需要有一定的空闲内存，拷贝也有开销，用于minor collection。<br />
第二个是mark-compact，将活的对象标记出来，然后搬迁到一起连成大块的内存，其他内存就可以回收了。这种方法不需要额外的空间，速度会慢一些，方法用于major collection。<br />
注意这两种GC机制都不是并行的，是单线程的，GC运行时，所有的用户线程将暂停，Java应用程序不做任何工作，web应用停止响应。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
JDK 1.4.1 中新的收集器都是为解决多处理器系统中垃圾收集器的问题而设计的。因为大多数垃圾收集算法会在一段时间里使系统停止，单线程的收集器很快会成为伸缩性瓶颈，因为在垃圾收集器将用户程序线程挂起时，除了一个处理器之外，其他的处理器都是空闲的。新收集器中的两个――并行复制收集器和并发标记-清除收集器――设计为减少收集暂停时间。另一个是并行清除收集器，它是为在大堆上的更高吞吐能力而设计的。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
并行复制收集器用 JVM 选项 -XX:+UseParNewGC 启用，是一个年轻代复制收集器，它将垃圾收集的工作分为与 CPU 数量一样多的线程。并发标记-清除收集器由 -XX:+UseConcMarkSweepGC 选项启用，它是一个老代标记-清除收集器，它在初始标记阶段（及在以后暂短重新标记阶段）暂短地停止整个系统，然后恢复用户程序，同时垃圾收集器线程与用户程序并发地执行。并行复制收集器和并发标记-清除收集器基本上是默认的复制收集器和标记-整理收集器的并发版本。由 -XX:+UseParallelGC 启用的并行清除收集器是年轻代收集器，针对多处理器系统上非常大(吉字节以及更大的)堆进行了优化。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
JDK 1.4.1 还包括大量的微调垃圾收集的选项。调整这些选项并衡量它们的效果可能会花费您大量时间，因此在试图微调垃圾收集器之前先对您的应用程序进行彻底的配置（profile）和优化，这样您的微调工作可能会得到更好的结果。<br />
微调垃圾收集首先要做的是检查冗长的 GC 输出。这会使您得到垃圾收集操作的频率、定时和持续时间等信息。最简单的垃圾收集微调就是扩大最大堆的大小（ -Xmx ）。随着堆的增大，复制收集会变得更有效，所以在增大堆时，您就减少了每个对象的收集成本。除了增加最大堆的大小，还可以用选项 -XX:NewRatio 增加分配给年轻代的空间份额。也可以用 -Xmn 选项显式指定年轻代的大小。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
<font color="#ff3300">注意：大的收集（major collection）既会收集年轻的代，也会收集老的代。 System.gc() 方法总是触发一个大的收集，这就是应该尽量少用（如果不能完全不用的话） System.gc() 的原因之一，因为大的收集要比小的收集花费长得多的时间。没有办法以编程方式触发小的收集，最好使用-XX:+DisableExplicitGC禁止ExplicitGC。</font></p>
<span  style="font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; font-size: 13px; "><font color="#ff3300"></font></span>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><font color="#ff3300"><br />
</font>JVM的很多参数会影响里面各部分空间的分配。-XX:MinHeapFreeRatio与-XX:MaxHeapFreeRatio设定空闲内存占总内存的比例范围，这两个参数会影响GC的频率和单次GC的耗时。-XX:NewRatio决定young与old generation的比例，设置-XX:NewRatio=3意味着old和 young generation是1:3。Young generation空间越大，minor collection频率越低，但是old generation空间小了，又可能导致major collection频率增加。一般来说，eden大小在maximum heap大小的1/4到1/3之间，old generation的大小一定要大于young generation。<br />
-XX:NewSize和-XX:MaxNewSize直接指定了young generation的最小值和最大值。</p>
<p style="font-size: 11px; font-family: Verdana, tahoma, Arial, Helvetica, sans-serif; color: #000000; "><br />
<font color="#ff0000">如果是Server-Side应用，请加 -server 参数。这样，缺省的NewRatio 是2，SurvivorRatio 是 25 ，适合大部分应用。也可以用NewSize、MaxNewSize来设置。 设置- Xms 和 -Xmx 的大小相等，可以避免在每次 GC 后调整堆内存的大小。同样道理设置NewSize、MaxNewSize相等。<br />
对于web应用还要注意，由于经常生成和加载类，permanent generation对GC的影响较大，应设置的大些。如果应用程序中有长期活对象，尽可能减少这些对象的存在期。例如，调整 HTTP 会话超时值将有助于更快地回收空闲会话对象。存泄漏的一个例子是在应用服务器中使用数据库连接池。当使用连接池时，必须在 finally 块中显式关闭 JDBC 语句和结果集对象。这是因为，当从池中调用连接对象上的 close() 时，只是简单地把连接返回池中以供重用，并没有实际关闭连接和关联的语句/结果集对象。如果使用全局缓存，最好使用 Week Reference，例如用 WeakHashMap 代替 HashMap 。</font></p>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/336381.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-10-28 14:45 <a href="http://www.blogjava.net/mlzry0612/articles/336381.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java的字符流和字节流</title><link>http://www.blogjava.net/mlzry0612/articles/329017.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Mon, 16 Aug 2010 09:16:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/329017.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/329017.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/329017.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/329017.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/329017.html</trackback:ping><description><![CDATA[<div class="blog_title" style="margin-bottom: 15px; font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">
<h3 style="font-size: 16px; line-height: 1.5em; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; padding-top: 10px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: black; "><br />
<br />
</h3>
<strong style="font-weight: bold; "></strong></div>
<div class="blog_content" style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><br />
</div>
<div class="blog_content" style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; "><span style="font-family: 宋体, 'Arial Narrow', tahoma, arial, 宋体, sans-serif; line-height: 21px; font-size: 14px; ">
<p style="margin-top: 0px; margin-right: 3px; margin-bottom: 15px; margin-left: 3px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; ">对于我们常用的GBK中，英文是占用1个字节，中文是2个</p>
<p style="margin-top: 0px; margin-right: 3px; margin-bottom: 15px; margin-left: 3px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; ">　　对于UTF-8，英文是1个，中文是3个</p>
<p style="margin-top: 0px; margin-right: 3px; margin-bottom: 15px; margin-left: 3px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; ">　　对于Unicode，英文中文都是2个</p>
<p style="margin-top: 0px; margin-right: 3px; margin-bottom: 15px; margin-left: 3px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; ">　　Java的流操作分为字节流和字符流两种。</p>
<p style="margin-top: 0px; margin-right: 3px; margin-bottom: 15px; margin-left: 3px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; "><span  style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; line-height: normal; font-size: medium; ">
<h5 style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; ">2.3 Java 中相关实现方法</h5>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; font-size: 12px; line-height: 16px; ">字符串类 String 中的内容是 UNICODE 字符串：</p>
<table border="1" cellspacing="0" bgcolor="#EEEEEE" cellpadding="6">
    <tbody>
        <tr>
            <td class="code" style="font-family: 'Lucida Console', SimSun, 'MS Gothic', 'New Gulim'; font-size: 13px; line-height: 16px; word-wrap: break-word; word-break: break-all; "><span class="rem" style="color: #339933; ">// Java 代码，直接写中文</span><span class="pw" style="color: #ff0000; "><br />
            String</span>&nbsp;string =&nbsp;<span class="string" style="color: #ff00ff; ">"中文123"</span>;<br />
            <br />
            <span class="rem" style="color: #339933; ">// 得到长度为 5，因为是 5 个字符</span><br />
            <span class="pw" style="color: #ff0000; ">System</span>.out.println(string.length());</td>
        </tr>
    </tbody>
</table>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; font-size: 12px; line-height: 16px; ">字符串 I/O 操作，字符与字节转换操作。在 Java 包 java.io.* 中，以&#8220;Stream&#8221;结尾的类一般是用来操作&#8220;字节串&#8221;的类，以&#8220;Reader&#8221;，&#8220;Writer&#8221;结尾的类一般是用来操作&#8220;字符串&#8221;的类。</p>
<table border="1" cellspacing="0" bgcolor="#EEEEEE" cellpadding="6">
    <tbody>
        <tr>
            <td class="code" style="font-family: 'Lucida Console', SimSun, 'MS Gothic', 'New Gulim'; font-size: 13px; line-height: 16px; word-wrap: break-word; word-break: break-all; "><span class="rem" style="color: #339933; ">// 字符串与字节串间相互转化<br />
            <br />
            // 按照 GB2312 得到字节（得到多字节字符串）</span><span class="key" style="color: #0000ff; "><br />
            byte</span>&nbsp;[] bytes = string.getBytes(<span class="string" style="color: #ff00ff; ">"GB2312"</span>);<br />
            <br />
            <span class="rem" style="color: #339933; ">// 从字节按照 GB2312 得到 UNICODE 字符串</span><br />
            string =&nbsp;<span class="key" style="color: #0000ff; ">new</span>&nbsp;<span class="pw" style="color: #ff0000; ">String</span>(bytes,&nbsp;<span class="string" style="color: #ff00ff; ">"GB2312"</span>);<br />
            <br />
            <span class="rem" style="color: #339933; ">// 要将 String 按照某种编码写入文本文件，有两种方法：<br />
            <br />
            // 第一种办法：用 Stream 类写入已经按照指定编码转化好的字节串</span><br />
            OutputStream os =&nbsp;<span class="key" style="color: #0000ff; ">new</span>&nbsp;FileOutputStream(<span class="string" style="color: #ff00ff; ">"1.txt"</span>);<br />
            os.write(bytes);<br />
            os.close();<br />
            <br />
            <span class="rem" style="color: #339933; ">// 第二种办法：构造指定编码的 Writer 来写入字符串</span><br />
            Writer ow =&nbsp;<span class="key" style="color: #0000ff; ">new</span>&nbsp;OutputStreamWriter(<span class="key" style="color: #0000ff; ">new</span>&nbsp;FileOutputStream(<span class="string" style="color: #ff00ff; ">"2.txt"</span>),&nbsp;<span class="string" style="color: #ff00ff; ">"GB2312"</span>);<br />
            ow.write(string);<br />
            ow.close();<br />
            <br />
            <span class="rem" style="color: #339933; ">/* 最后得到的 1.txt 和 2.txt 都是 7 个字节 */</span></td>
        </tr>
    </tbody>
</table>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; font-size: 12px; line-height: 16px; ">如果 java 的源程序编码与当前默认 ANSI 编码不符，则在编译的时候，需要指明一下源程序的编码。比如：</p>
<table border="1" cellspacing="0" bgcolor="#EEEEEE" cellpadding="6">
    <tbody>
        <tr>
            <td class="code" style="font-family: 'Lucida Console', SimSun, 'MS Gothic', 'New Gulim'; font-size: 13px; line-height: 16px; word-wrap: break-word; word-break: break-all; ">E:\&gt;javac&nbsp;<font color="#FF0000">-encoding BIG5</font>&nbsp;Hello.java</td>
        </tr>
    </tbody>
</table>
<p style="font-family: Verdana, Arial, Helvetica, sans-serif, SimSun, 'MS Gothic', 'New Gulim'; font-size: 12px; line-height: 16px; ">以上需要注意区分源程序的编码与 I/O 操作的编码，前者是在编译时起作用，后者是在运行时起作用。</p>
</span></p>
</span></div>
<div class="blog_content" style="font-family: Arial, sans-serif, Helvetica, Tahoma; font-size: 12px; line-height: 18px; ">IO分两种流&nbsp;<br />
<br />
字节流 InputStream OutputStream&nbsp;<br />
<br />
字符流 Reader Writer&nbsp;<br />
<br />
他们都是抽象类&nbsp;<br />
<br />
具体实现&nbsp;<br />
字节流 FileInputStream FileOutputStream&nbsp;<br />
字符流 FileReader FileWriter&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 字符流处理的单元为2个字节的Unicode字符，分别操作字符、字符数组或字符串，而字节流处理单元为1个字节， 操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的，所以它对多国语言支持性比较好！如果是 音频文件、图片、歌曲，就用字节流好点，如果是关系到中文（文本）的，用字符流好点.&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; 所有文件的储存是都是字节（byte）的储存，在磁盘上保留的并不是文件的字符而是先把字符编码成字节，再储存这些字节到磁盘。在读取文件（特别是文本文件）时，也是一个字节一个字节地读取以形成字节序列.&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 字节流可用于任何类型的对象，包括二进制对象，而字符流只能处理字符或者字符串； 2. 字节流提供了处理任何类型的IO操作的功能，但它不能直接处理Unicode字符，而字符流就可以。&nbsp;<br />
<br />
<br />
字节流转换成字符流可以用 InputSteamReader OutputStreamWriter&nbsp;<br />
<br />
转换成BufferdReader BufferedWriter 他们具有缓冲区&nbsp;<br />
<br />
例如：读取文件 从字节流输入到字符流输入&nbsp;<br />
定义一个字节流：&nbsp;<br />
FileInputStream fileInputStream = new FileInputStream("d:/text.txt"); // 定义一个指&nbsp;<br />
<br />
向D:/TEXT.TXT 的字节流&nbsp;<br />
<br />
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);&nbsp;<br />
//字节流转换成InputStreamReader&nbsp;<br />
BufferedReader bufferedReader = new BufferedReader(inputSteamReader);&nbsp;<br />
//InputStreamReader 转换成带缓存的bufferedReader&nbsp;<br />
<br />
可以把读出来的内容赋值给字符&nbsp;<br />
<br />
String ss = new String();&nbsp;<br />
String s;&nbsp;<br />
while((s = bufferedReader.readLine())!=null){&nbsp;<br />
ss += s;&nbsp;<br />
}&nbsp;<br />
<br />
例如：写入文件 从字节流输出到字符流输出&nbsp;<br />
<br />
FileOutputStream fileOutputStream = new FileOutputStream("d:/text.txt"); //定义一个&nbsp;<br />
<br />
指向D:/TEXT.TXT文件&nbsp;<br />
<br />
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);&nbsp;<br />
<br />
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);&nbsp;<br />
<br />
bufferedWriter.write(s);&nbsp;<br />
<br />
bufferedWriter.close();&nbsp;<br />
outputStreamWriter.close();&nbsp;<br />
fileOutputStream.close();&nbsp;<br />
<br />
<br />
例程：&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp; 将字符串转化为字节流#region 将字符串转化为字节流&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /**//// &lt;summary&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// 将字符串转化为字节流&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;/summary&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;param name="_Source"&gt;字串&lt;/param&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;returns&gt;字节流&lt;/returns&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static byte[] String2Bytes(string strSource)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.MemoryStream&nbsp;&nbsp; memoryStream=new&nbsp;&nbsp; System.IO.MemoryStream();&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.BinaryWriter&nbsp;&nbsp; binaryWriter=new&nbsp;&nbsp; System.IO.BinaryWriter(memoryStream);&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; binaryWriter.Write( strSource );&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[]&nbsp;&nbsp; buffer=memoryStream.GetBuffer();&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return buffer;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion&nbsp;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 将字节流转化为字符串#region 将字节流转化为字符串&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /**//// &lt;summary&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// 将字节流转化为字符串&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;/summary&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;param name="bytData"&gt;字节流&lt;/param&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /// &lt;returns&gt;字串&lt;/returns&gt;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public static string Bytes2String(byte[] bytData)&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //字节流-&gt;字符串&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.MemoryStream&nbsp;&nbsp; memoryStream2 = new&nbsp;&nbsp; System.IO.MemoryStream(bytData);&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.BinaryReader&nbsp;&nbsp; binaryReader = new&nbsp;&nbsp; System.IO.BinaryReader(memoryStream2);&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string&nbsp;&nbsp; s2=binaryReader.ReadString();&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return s2;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endregion</div>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/329017.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-08-16 17:16 <a href="http://www.blogjava.net/mlzry0612/articles/329017.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>主流数据库JDBC连接</title><link>http://www.blogjava.net/mlzry0612/articles/328975.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Mon, 16 Aug 2010 05:30:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/328975.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/328975.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/328975.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/328975.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/328975.html</trackback:ping><description><![CDATA[<span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">
<div>其实，只是一个API熟悉度的问题，有个同事面试新来的伙子，问的一楞楞的，自己总结一下：</div>
1 将数据库的JDBC驱动加载到classpath中，在基于JAVAEE的WEB应用实际开发过程中，通常要把目标数据库产品的JDBC驱动复制到WEB-INF/lib下.</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">2 加载JDBC驱动，并将其注册到DriverManager中，下面是一些主流数据库的JDBC驱动加裁注册的代码:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Oracle8/8i/9iO数据库(thin模式)</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Sql Server7.0/2000数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //DB2数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Informix数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forName("com.informix.jdbc.IfxDriver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Sybase数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forName("com.sybase.jdbc.SybDriver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //MySQL数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forName("com.mysql.jdbc.Driver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //PostgreSQL数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Class.forNaem("org.postgresql.Driver").newInstance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">3 建立数据库连接，取得Connection对象.例如:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Oracle8/8i/9i数据库(thin模式)</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:oracle:thin:@localhost:1521:orcl";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String user="scott";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String password="tiger";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url,user,password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Sql Server7.0/2000数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String user="sa";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String password="";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url,user,password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //DB2数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:db2://localhost:5000/sample";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String user="amdin"</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String password=-"";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url,user,password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Informix数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:informix-sqli://localhost:1533/testDB:INFORMIXSERVER=myserver;user=testuser;password=testpassword";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //Sybase数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:sybase:Tds:localhost:5007/tsdata";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Properties sysProps=System.getProperties();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; SysProps.put("user","userid");</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; SysProps.put("password","user_password");</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url,SysProps);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //MySQL数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:mysql://localhost:3306/testDB?user=root&amp;password=root&amp;useUnicode=true&amp;characterEncoding=gb2312";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //PostgreSQL数据库</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String url="jdbc:postgresql://localhost/testDB";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String user="myuser";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String password="mypassword";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Connection conn=DriverManager.getConnection(url,user,password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">4 建立Statement对象或PreparedStatement对象.例如：</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //建立Statement对象</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; Statement stmt=conn.createStatement();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //建立ProparedStatement对象</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String sql="select * from user where userName=? and password=?";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; PreparedStatement pstmt=Conn.prepareStatement(sql);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; pstmt.setString(1,"admin");</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; pstmt.setString(2,"liubin");</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">5 执行SQL语句.例如：</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; String sql="select * from users";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; ResultSet rs=stmt.executeQuery(sql);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //执行动态SQL查询</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; ResultSet rs=pstmt.executeQuery();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; //执行insert update delete等语句，先定义sql</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; stmt.executeUpdate(sql);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">6 访问结果记录集ResultSet对象。例如：</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; while(rs.next)</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; {</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; out.println("你的第一个字段内容为："+rs.getString());</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; out.println("你的第二个字段内容为："+rs.getString(2));</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; }</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭，释放所占用的资源.例如:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; rs.close();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; stmt.clost();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; pstmt.close();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp; con.close();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;后加上</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">MySQL：&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.mysql.jdbc.Driver";&nbsp;&nbsp;&nbsp; //驱动程序</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:mysql://localhost:3306/db_name";&nbsp;&nbsp;&nbsp; //连接的URL,db_name为数据库名&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).new Instance();</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">Microsoft SQL Server 2.0驱动(3个jar的那个):</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";&nbsp;&nbsp;&nbsp; //连接SQL数据库的方法</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";&nbsp;&nbsp;&nbsp; //db_name为数据库名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).new Instance();&nbsp;&nbsp;&nbsp; //加载数据可驱动</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,UserName,Password);&nbsp;&nbsp;&nbsp; //</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">Microsoft SQL Server 3.0驱动(1个jar的那个): // 老紫竹完善</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";&nbsp;&nbsp;&nbsp; //连接SQL数据库的方法</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_name";&nbsp;&nbsp;&nbsp; //db_name为数据库名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).new Instance();&nbsp;&nbsp;&nbsp; //加载数据可驱动</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,UserName,Password);&nbsp;&nbsp;&nbsp; //</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">Sysbase:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.sybase.jdbc.SybDriver";&nbsp;&nbsp;&nbsp; //驱动程序</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:Sysbase://localhost:5007/db_name";&nbsp;&nbsp;&nbsp; //db_name为数据可名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">Oracle(用thin模式):</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="oracle.jdbc.driver.OracleDriver";&nbsp;&nbsp;&nbsp; //连接数据库的方法</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:oracle:thin:@loaclhost:1521:orcl";&nbsp;&nbsp;&nbsp; //orcl为数据库的SID</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp; //加载数据库驱动</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">PostgreSQL:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="org.postgresql.Driver";&nbsp;&nbsp;&nbsp; //连接数据库的方法</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:postgresql://localhost/db_name";&nbsp;&nbsp;&nbsp; //db_name为数据可名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">DB2：</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.ibm.db2.jdbc.app.DB2.Driver";&nbsp;&nbsp;&nbsp; //连接具有DB2客户端的Provider实例</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; //String Driver="com.ibm.db2.jdbc.net.DB2.Driver";&nbsp;&nbsp;&nbsp; //连接不具有DB2客户端的Provider实例</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:db2://localhost:5000/db_name";&nbsp;&nbsp;&nbsp; //db_name为数据可名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">Informix:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="com.informix.jdbc.IfxDriver";&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:Informix-sqli://localhost:1533/db_name:INFORMIXSER=myserver";&nbsp;&nbsp;&nbsp; //db_name为数据可名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">JDBC-ODBC:</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Driver="sun.jdbc.odbc.JdbcOdbcDriver";</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String URL="jdbc:odbc:dbsource";&nbsp;&nbsp;&nbsp; //dbsource为数据源名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Username="username";&nbsp;&nbsp;&nbsp; //用户名</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; String Password="password";&nbsp;&nbsp;&nbsp; //密码</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Class.forName(Driver).newInstance();&nbsp;&nbsp;&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; "><br />
</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;&nbsp;&nbsp; Connection con=DriverManager.getConnection(URL,Username,Password);</span><span style="font-family: Tahoma; font-size: 12px; line-height: 18px; ">&nbsp;</span>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/328975.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2010-08-16 13:30 <a href="http://www.blogjava.net/mlzry0612/articles/328975.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>获得当前运行jar包存放路径的方法</title><link>http://www.blogjava.net/mlzry0612/articles/307235.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Fri, 25 Dec 2009 02:09:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/307235.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/307235.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/307235.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/307235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/307235.html</trackback:ping><description><![CDATA[<h2><a class="entrytitlelist" title="获得jar包存放路径的方法" href="http://www.zeali.net/entry/404" rel="bookmark">获得运行jar包存放路径的方法</a></h2>
<div class="modcontent">在 java 中， <font face="courier new,courier,monospace"><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html" target="_blank">System</a>.getProperty("user.dir")</font> 得到的是当前工作路径（对应命令行命令就是 linux 下的 pwd 、 windows 下的 cd ），而 <font face="courier new,courier,monospace">System.getProperty("user.home")</font> 得到的是当前用户主目录（对应于 linux 下的 ~ 目录、 windows 下的 document and settings\username ）；但如果你想得到所运行的 java 程序所在的路径，似乎并没有什么简单的方法。
<p>　　比如说，有一个 pathTest.jar 包放在 D:\zeal\webdev 下，这个 jar 执行的时候需要在当前目录下读写一个 test.log 文件。如果不采用绝对路径来读写 test.log 的话，不同的 jar 运行方式会带来不同的结果：</p>
<p><font face="courier new,courier,monospace">cd D:\zeal\webdev<br />
java -jar pathTest.jar</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>( test.log 将生成在 D:\zeal\webdev 目录下 )</em></p>
<p><font face="courier new,courier,monospace">cd E:\sun\jre\bin<br />
java -jar D:\zeal\webdev\pathTest.jar</font><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>( test.lgo 将生成在 E:\sun\jre\bin 目录下 )</em></p>
<p>　　这势必造成潜在的运行错误。解决的方法是必须在程序中指定 test.log 文件的绝对路径。从本意上来讲，这里我们希望 test.log 与 pathTest.jar 位于相同的路径之下。但显然无论是 "user.dir" 还是 "user.home" 都不会返回正确的结果。</p>
<p>　　幸好， java 允许我们定位某个 class 的 <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/CodeSource.html" target="_blank">CodeSource</a> ， 通过 CodeSource 提供的 Location 信息就能够找到相关的文件路径。假设 pathTest.jar 的 main class&nbsp;是 pathTest ，通过</p>
<p><font face="courier new,courier,monospace">new pathTest().<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#getClass()" target="_blank">getClass()</a>.<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html#getProtectionDomain()" target="_blank">getProtectionDomain()</a>.<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/ProtectionDomain.html#getCodeSource()" target="_blank">getCodeSource()</a>.<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/CodeSource.html#getLocation()" target="_blank">getLocation()</a>;</font></p>
<p><font face="Courier New">　　就可以得到 pathTest.jar 的完整路径（ file:/D:/zeal/webdev/pathTest.jar ），接下来怎么做就简单了。当然，实际应用中一般我们会选择把所有与jar相关的资源文件一起打包进去，或者使用 "user.home" 这样的固定位置来存放；除非在特殊情况下才会需要通过这个迂回的方式来获取相关的路径信息。</font></p>
</div>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/307235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2009-12-25 10:09 <a href="http://www.blogjava.net/mlzry0612/articles/307235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ClassLoaderUtil </title><link>http://www.blogjava.net/mlzry0612/articles/307212.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Thu, 24 Dec 2009 13:04:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/307212.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/307212.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/307212.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/307212.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/307212.html</trackback:ping><description><![CDATA[<span style="color: #0000ff">This class is used to dynmaic add user specify location to system loader or external loader<br />
<p><font style="background-color: #cce8cf">/* <br />
&nbsp;* $Id: ClassLoaderUtil.java, Dec 22, 2009, 8:22:21 PM, liama. Exp$<br />
&nbsp;*<br />
&nbsp;*/<br />
</font></p>
<p><font style="background-color: #cce8cf">import java.io.File;<br />
import java.io.PrintStream;<br />
import java.lang.reflect.Field;<br />
import java.lang.reflect.InvocationTargetException;<br />
import java.lang.reflect.Method;<br />
import java.net.MalformedURLException;<br />
import java.net.URL;<br />
import java.net.URLClassLoader;<br />
import java.util.List;</font></p>
<p><font style="background-color: #cce8cf">import sun.misc.Launcher;</font></p>
<p><font style="background-color: #cce8cf">/**<br />
&nbsp;* Class/Interface description<br />
&nbsp;* <br />
&nbsp;* @author liama&lt;/a&gt;<br />
&nbsp;* @since JDK 1.5<br />
&nbsp;*/<br />
public class ClassLoaderUtil {<br />
&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * The value is used for get parent loader.<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; private static Field classes;</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * The add url method loader.<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; private static Method addURL;<br />
&nbsp;&nbsp;&nbsp; static {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; classes = ClassLoader.class.getDeclaredField("parent");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; classes.setAccessible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL.setAccessible(true);<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * The value is used for system loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; private static URLClassLoader system = (URLClassLoader) getSystemClassLoader();</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * The value is used for external loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; private static URLClassLoader ext = (URLClassLoader) getExtClassLoader();</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get system loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link ClassLoader}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static ClassLoader getSystemClassLoader() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ClassLoader.getSystemClassLoader();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get external loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link ClassLoader}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static ClassLoader getExtClassLoader() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getSystemClassLoader().getParent();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get sub class loader from system class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link List}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static List getClassesLoadedBySystemClassLoader() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getClassesLoadedByClassLoader(getSystemClassLoader());<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get sub class loader from external class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link List}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static List getClassesLoadedByExtClassLoader() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getClassesLoadedByClassLoader(getExtClassLoader());<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get sub class loader from&nbsp; class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param cl {@link ClassLoader}<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link List}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static List getClassesLoadedByClassLoader(ClassLoader cl) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (List) classes.get(cl);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //throw new RootException(e);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return null;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get bootstrap urls<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getBootstrapURLs() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Launcher.getBootstrapClassPath().getURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get system urls<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getSystemURLs() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return system.getURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get external urls<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getExtURLs() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ext.getURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Print class path to specify stream<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param ps {@link PrintStream}<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param classPath {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; private static void list(PrintStream ps, URL[] classPath) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; classPath.length; i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.println(classPath[i]);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * List boot strap class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listBootstrapClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listBootstrapClassPath(System.out);<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Print boot strap class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param ps {@link PrintStream}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listBootstrapClassPath(PrintStream ps) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.println("BootstrapClassPath:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list(ps, getBootstrapClassPath());<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * list system class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listSystemClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listSystemClassPath(System.out);<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Print system class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param ps {@link PrintStream}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listSystemClassPath(PrintStream ps) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.println("SystemClassPath:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list(ps, getSystemClassPath());<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * List external class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listExtClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; listExtClassPath(System.out);<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Print external class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param ps {@link PrintStream}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void listExtClassPath(PrintStream ps) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ps.println("ExtClassPath:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list(ps, getExtClassPath());<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get boot strap class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getBootstrapClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getBootstrapURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get system class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getSystemClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getSystemURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Get external class path<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @return {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static URL[] getExtClassPath() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return getExtURLs();<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify URL to system class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param url {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addURL2SystemClassLoader(URL url) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL.invoke(system, new Object[] { url });<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify URL to external class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param url {@link URL}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addURL2ExtClassLoader(URL url) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL.invoke(ext, new Object[] { url });<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify String to class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param path {@link String}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addClassPath(String path) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addClassPath(new File(path));<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify String to external class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param path {@link String}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addExtClassPath(String path) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addExtClassPath(new File(path));<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify File to&nbsp; class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param dirOrJar {@link File}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addClassPath(File dirOrJar) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL2SystemClassLoader(dirOrJar.toURL());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (MalformedURLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //throw new RootException(e);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }</font></p>
<p><font style="background-color: #cce8cf">&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add user specify File to external class loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param dirOrJar {@link File}<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addExtClassPath(File dirOrJar) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL2ExtClassLoader(dirOrJar.toURL());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (MalformedURLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add specify path to specify loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param cl {@link ClassLoader}<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param path String<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addClassPath2ClassLoader(ClassLoader cl, String path){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL.invoke(cl, new File(path).toURL());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IllegalArgumentException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (MalformedURLException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IllegalAccessException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InvocationTargetException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; <br />
&nbsp;&nbsp;&nbsp; /**<br />
&nbsp;&nbsp;&nbsp;&nbsp; * Add specify path to specify loader<br />
&nbsp;&nbsp;&nbsp;&nbsp; * <br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param cl {@link ClassLoader}<br />
&nbsp;&nbsp;&nbsp;&nbsp; * @param path String<br />
&nbsp;&nbsp;&nbsp;&nbsp; */<br />
&nbsp;&nbsp;&nbsp; public static void addClassPath2ClassLoader(ClassLoader cl, URL path){<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addURL.invoke(cl, path);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IllegalArgumentException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (IllegalAccessException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (InvocationTargetException e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; }<br />
}</font></p>
</span>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/307212.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2009-12-24 21:04 <a href="http://www.blogjava.net/mlzry0612/articles/307212.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Difference  between OOA and OOD</title><link>http://www.blogjava.net/mlzry0612/articles/263137.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Tue, 31 Mar 2009 07:37:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/263137.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/263137.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/263137.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/263137.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/263137.html</trackback:ping><description><![CDATA[OOA与OOD的区别 <br />
上讨论了OOA阶段和OOD阶段所应完成的主要工作,由于这两个阶段的划分没有公认 <br />
的标准,有些工作是在OOA阶段完成还是在OOD阶段完成还存在着争议。有人认为OOA和 <br />
OOD可以交叉进行;有人认为OOD是对OOA结果的改进和细化,所以只提OOA;有人则更 <br />
强调OOD。尽管OOA和OOD存在着某些交叉和联系,但它们之间仍有许多差别,如: <br />
1)OOA将现实世界中的实体抽象为问题对象,并构造问题域中的系统需求模型;OOD将 <br />
问题对象转化为解域中的类并在解域中构造出问题的解。 <br />
2)OOA侧重于用户需求的分析和对问题域的理解,分析人员关心的是系统结构及对象间 <br />
的关系;OOD则侧重于系统的实现,设计人员关心的是对象的行为及其实现。 <br />
3)OOA标识了一组对象,并通过其相互作用来刻划系统,该阶段的工作与程序设计语言 <br />
无关;OOD定义了一组类,并设计出系统的实现蓝图,概要设计与程序设计语言无关,但详细 <br />
设计则与之有比较密切的联系。( <br />
4)OOA识别的对象是对客观世界实体的抽象,标识对象的准则是:该对象的引入是否有助于对问题域的理解;OOD中构造类的准则是:该类的构造是否可行,是否有效地实现了抽象 <br />
数据类型,是否有助于系统的实现和提高软件质量。( <br />
5)两个阶段都没有提及系统对象,但原因不同。在OOA阶段,分析与实现无关,分析所涉 <br />
及的范围与解域无关,系统对象自然不用考虑。OOD建立的对象模型本身就是要设计的软件 <br />
系统,对系统对象的考虑是隐含的。 <br />
6)组装结构和分类结构在两个阶段所起的作用不同。在OOA阶段,它们的引入主要是为 <br />
了理解问题;而在OOD阶段,它们的引入则主要是针对软件的构造和实现。分类结构通过继 <br />
承机制来实现,因而代码得到了有效地复用;组装结构则将一些类组合在一起构成较大的软件 <br />
构件。 <br />
7)OOA并没有考虑对象的产生问题,当其对应的实体在现实世界中出现时,它也就在问 <br />
题域中产生了。OOA也不考虑对象属性的取值和服务算法的实现。而在OOD阶段这些问题 <br />
都必须详细考虑。 <br />
8)OOD涉及到重载问题;而OOA没有考虑,因为考虑过多的实现细节对理解问题和分 <br />
析用户需求没有多大帮助。
 <img src ="http://www.blogjava.net/mlzry0612/aggbug/263137.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2009-03-31 15:37 <a href="http://www.blogjava.net/mlzry0612/articles/263137.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>synchronized 误区解疑</title><link>http://www.blogjava.net/mlzry0612/articles/263064.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Tue, 31 Mar 2009 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/263064.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/263064.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/263064.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/263064.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/263064.html</trackback:ping><description><![CDATA[<p>实际上，一些新手对于对方法加锁和对对象加锁还有误区。其实这个和多线程以及多个类的实例是有决定性的关系的. </p>
<p>1、synchronized关键字的作用域有二种：<br />
<br />
1）是某个对象实例内，synchronized aMethod(){}可以防止多个线程同时访问这个对象的synchronized方法（如果一个对象有多个synchronized方法，只要一个线程访问了其中的一个synchronized方法，其它线程不能同时访问这个对象中任何一个synchronized方法）。<span style="color: #ff0000">这时，不同的对象实例的synchronized方法是不相干扰的。也就是说，其它线程照样可以同时访问相同类的另一个对象实例中的synchronized方法；<br />
</span><br />
2）是某个类的范围，<span style="color: #ff0000">synchronized static aStaticMethod{}防止多个线程同时访问这个类中的synchronized static 方法。它可以对类的所有对象实例起作用。</span>2、除了方法前用synchronized关键字，synchronized关键字还可以用于方法中的某个区块中，表示只对这个区块的资源实行互斥访问。用法是: synchronized(this){/*区块*/}，它的作用域是当前对象；</p>
<p>3、synchronized关键字是不能继承的，也就是说，基类的方法synchronized f(){} 在继承类中并不自动是synchronized f(){}，而是变成了f(){}。继承类需要你显式的指定它的某个方法为synchronized方法；</p>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/263064.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2009-03-31 10:56 <a href="http://www.blogjava.net/mlzry0612/articles/263064.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK6.0的新特性</title><link>http://www.blogjava.net/mlzry0612/articles/231976.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Wed, 01 Oct 2008 05:10:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/231976.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/231976.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/231976.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/231976.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/231976.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: JDK6.0发布有段时间了，新的JDK也有不少新的特性，我去网上搜集了一下，列在下面和大家一起学习．１．Desktop和SystemTray. 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/231976.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/231976.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2008-10-01 13:10 <a href="http://www.blogjava.net/mlzry0612/articles/231976.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )</title><link>http://www.blogjava.net/mlzry0612/articles/223420.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Thu, 21 Aug 2008 03:00:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/223420.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/223420.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/223420.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/223420.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/223420.html</trackback:ping><description><![CDATA[<p><span style="font-size: small"><br />
这两天看了一下深入浅出JVM这本书，推荐给高级的java程序员去看，对你了解JAVA的底层和运行机制有<br />
比较大的帮助。<br />
废话不想讲了.入主题：<br />
<br />
先了解具体的概念：<br />
JAVA的JVM的内存可分为3个区：堆(heap)、栈(stack)和方法区(method)</span> </p>
<p><span style="font-size: small">堆区:<br />
1.存储的全部是对象，每个对象都包含一个与之对应的class的信息。(class的目的是得到操作指令)<br />
2.<span style="color: #ff0000">jvm只有一个堆区(heap)被所有线程共享，</span>堆中不存放基本类型和对象引用，只存放对象本身<br />
栈区:<br />
1.<span style="color: #ff0000">每个线程包含一个栈区，</span>栈中只保存基础数据类型的对象和自定义对象的引用(不是对象)，对象都存放在堆区中<br />
2.每个栈中的数据(原始类型和对象引用)都是私有的，其他栈不能访问。<br />
3.栈分为3个部分：基本类型变量区、执行环境上下文、操作指令区(存放操作指令)。<br />
方法区:<br />
1.又叫静态区，跟堆一样，<span style="color: #ff0000">被所有的线程共享。</span>方法区包含所有的class和static变量。<br />
2.方法区中包含的都是在整个程序中永远唯一的元素，如class，static变量。</span> <br />
<br />
<br />
为了更清楚地搞明白发生在运行时数据区里的黑幕，我们来准备2个小道具（2个非常简单的小程序）。<br />
<br />
AppMain.java<br />
<br />
</p>
<pre class="codeStyle"> public   class  AppMain&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//运行时, jvm 把appmain的信息都放入方法区<br />
{<br />
public   static   void  main(String[] args)  //main 方法本身放入方法区。<br />
{<br />
Sample test1 = new  Sample( " 测试1 " );   //test1是引用，所以放到栈区里， Sample是自定义对象应该放到堆里面<br />
Sample test2 = new  Sample( " 测试2 " );<br />
<br />
test1.printName();<br />
test2.printName();<br />
} <br />
} </pre>
<p><br />
<br />
Sample.java<br />
<br />
</p>
<pre class="codeStyle"> public   class  Sample        //运行时, jvm 把appmain的信息都放入方法区<br />
{<br />
/** 范例名称 */ <br />
private  name;      //new Sample实例后， name 引用放入栈区里，  name 对象放入堆里<br />
<br />
/** 构造方法 */ <br />
public  Sample(String name)<br />
{<br />
this .name = name;<br />
} <br />
<br />
/** 输出 */ <br />
public   void  printName()   //print方法本身放入 方法区里。<br />
{<br />
System.out.println(name);<br />
} <br />
} </pre>
<p><br />
OK，让我们开始行动吧，出发指令就是：&#8220;java AppMain&#8221;，包包里带好我们的行动向导图，Let&#8217;s GO！<br />
<br />
<img src="http://www.blogjava.net/images/blogjava_net/aimee/20823/o_Java虚拟机运行时数据区.jpg" border="0"  alt="" /><br />
<br />
系统收到了我们发出的指令，启动了一个Java虚拟机进程，这个进程首先从classpath中找到AppMain.class文件，读取这个文件中的二进制数据，然后把Appmain类的类信息存放到运行时数据区的方法区中。这一过程称为AppMain类的加载过程。<br />
接着，Java虚拟机定位到方法区中AppMain类的Main()方法的字节码，开始执行它的指令。这个main()方法的第一条语句就是：<br />
Sample test1=new Sample("测试1");<br />
语句很简单啦，就是让java虚拟机创建一个Sample实例，并且呢，使引用变量test1引用这个实例。貌似小case一桩哦，就让我们来跟踪一下Java虚拟机，看看它究竟是怎么来执行这个任务的：<br />
<br />
1、 Java虚拟机一看，不就是建立一个Sample实例吗，简单，于是就直奔方法区而去，先找到Sample类的类型信息再说。结果呢，嘿嘿，没找到@@，这会儿的方法区里还没有Sample类呢。可Java虚拟机也不是一根筋的笨蛋，于是，它发扬&#8220;自己动手，丰衣足食&#8221;的作风，立马加载了Sample类，把Sample类的类型信息存放在方法区里。<br />
<br />
2、 好啦，资料找到了，下面就开始干活啦。Java虚拟机做的第一件事情就是在堆区中为一个新的Sample实例分配内存, 这个Sample实例持有着指向方法区的Sample类的类型信息的引用。这里所说的引用，实际上指的是Sample类的类型信息在方法区中的内存地址，其实，就是有点类似于C语言里的指针啦~~，而这个地址呢，就存放了在Sample实例的数据区里。<br />
<br />
3、 在JAVA虚拟机进程中，每个线程都会拥有一个方法调用栈，用来跟踪线程运行中一系列的方法调用过程，栈中的每一个元素就被称为栈帧，每当线程调用一个方法的时候就会向方法栈压入一个新帧。这里的帧用来存储方法的参数、局部变量和运算过程中的临时数据。OK，原理讲完了，就让我们来继续我们的跟踪行动！位于&#8220;=&#8221;前的Test1是一个在main()方法中定义的变量，可见，它是一个局部变量，因此，它被会添加到了执行main()方法的主线程的JAVA方法调用栈中。而&#8220;=&#8221;将把这个test1变量指向堆区中的Sample实例，也就是说，它持有指向Sample实例的引用。<br />
<br />
OK，到这里为止呢，JAVA虚拟机就完成了这个简单语句的执行任务。参考我们的行动向导图，我们终于初步摸清了JAVA虚拟机的一点点底细了，COOL！<br />
<br />
接下来，JAVA虚拟机将继续执行后续指令，在堆区里继续创建另一个Sample实例，然后依次执行它们的printName()方法。当JAVA虚拟机执行test1.printName()方法时，JAVA虚拟机根据局部变量test1持有的引用，定位到堆区中的Sample实例，再根据Sample实例持有的引用，定位到方法去中Sample类的类型信息，从而获得printName()方法的字节码，接着执行printName()方法包含的指令。</p>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/223420.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2008-08-21 11:00 <a href="http://www.blogjava.net/mlzry0612/articles/223420.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>彻底转变流，第 2 部分：优化 Java 内部 I/O</title><link>http://www.blogjava.net/mlzry0612/articles/202376.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Fri, 23 May 2008 04:32:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/202376.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/202376.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/202376.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/202376.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/202376.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 彻底转变流，第 2 部分：优化 Java 内部 I/O            替换字节数组流和管道流                                                                                                                        ...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/202376.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/202376.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2008-05-23 12:32 <a href="http://www.blogjava.net/mlzry0612/articles/202376.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>彻底转变流，第 1 部分</title><link>http://www.blogjava.net/mlzry0612/articles/202375.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Fri, 23 May 2008 04:31:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/202375.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/202375.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/202375.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/202375.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/202375.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;                                    彻底转变流，第 1 部分            从输出流中读取                                                                                           ...&nbsp;&nbsp;<a href='http://www.blogjava.net/mlzry0612/articles/202375.html'>阅读全文</a><img src ="http://www.blogjava.net/mlzry0612/aggbug/202375.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2008-05-23 12:31 <a href="http://www.blogjava.net/mlzry0612/articles/202375.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Properties的读取</title><link>http://www.blogjava.net/mlzry0612/articles/117784.html</link><dc:creator>Daniel</dc:creator><author>Daniel</author><pubDate>Wed, 16 May 2007 03:00:00 GMT</pubDate><guid>http://www.blogjava.net/mlzry0612/articles/117784.html</guid><wfw:comment>http://www.blogjava.net/mlzry0612/comments/117784.html</wfw:comment><comments>http://www.blogjava.net/mlzry0612/articles/117784.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/mlzry0612/comments/commentRss/117784.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/mlzry0612/services/trackbacks/117784.html</trackback:ping><description><![CDATA[The UML project's code:<br><br>public void cc()<br>&nbsp;{<br>&nbsp;&nbsp;Properties pro = new Properties();<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;File file = new File<span style="COLOR: red">("./config/cc.properties</span>");<br>&nbsp;&nbsp;&nbsp;InputStream in = new BufferedInputStream(new FileInputStream(file));&nbsp;<br>&nbsp;&nbsp;&nbsp;pro.load(in);&nbsp;<br>&nbsp;&nbsp;&nbsp;System.out.println(pro.getProperty("tu"));<br>&nbsp;&nbsp;} catch (IOException e)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;// TODO Auto-generated catch block<br>&nbsp;&nbsp;&nbsp;e.printStackTrace();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;}<br><br><br>This suitation is used for properties file as same level with this class.<br>public void cc()<br>&nbsp;{<br>&nbsp;&nbsp;Properties pro = new Properties();<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;pro.load(this.getClass().getResourceAsStream("cc.properties"));<br>&nbsp;&nbsp;&nbsp;pro.load(in);&nbsp;<br>&nbsp;&nbsp;&nbsp;System.out.println(pro.getProperty("tu"));<br>&nbsp;&nbsp;} catch (IOException e)<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;// TODO Auto-generated catch block<br>&nbsp;&nbsp;&nbsp;e.printStackTrace();<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;<br>&nbsp;}<br><br><br>使用J2SE API读取Properties文件的六种方法 <br><br>1。使用java.util.Properties类的load()方法 <br>示例： <br><code>InputStream in = lnew BufferedInputStream(new FileInputStream(name)); <br>Properties p = new Properties(); <br>p.load(in); <br></code><br>2。使用java.util.ResourceBundle类的getBundle()方法 <br>示例：<br><code>ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); <br></code><br>3。使用java.util.PropertyResourceBundle类的构造函数 <br>示例： <br><code>InputStream in = new BufferedInputStream(new FileInputStream(name)); <br>ResourceBundle rb = new PropertyResourceBundle(in); <br></code><br>4。使用class变量的getResourceAsStream()方法 <br>示例： <br><code>InputStream in = JProperties.class.getResourceAsStream(name); <br>Properties p = new Properties(); <br>p.load(in); <br></code><br>5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 <br>示例： <br><code>InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); <br>Properties p = new Properties(); <br>p.load(in); <br></code><br>6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 <br>示例： <br><code>InputStream in = ClassLoader.getSystemResourceAsStream(name); <br>Properties p = new Properties(); <br>p.load(in); <br></code><br>补充 <br><br>Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法 <br>示例：<br><code>InputStream in = context.getResourceAsStream(path); <br>Properties p = new Properties(); <br>p.load(in); <br></code><br>
<img src ="http://www.blogjava.net/mlzry0612/aggbug/117784.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/mlzry0612/" target="_blank">Daniel</a> 2007-05-16 11:00 <a href="http://www.blogjava.net/mlzry0612/articles/117784.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>