﻿<?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-经验不在于年限，在于积累---专注互联网软件开发-随笔分类-Java基础</title><link>http://www.blogjava.net/hankchen/category/41467.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 14 Jan 2010 12:46:40 GMT</lastBuildDate><pubDate>Thu, 14 Jan 2010 12:46:40 GMT</pubDate><ttl>60</ttl><item><title>Google Collections之Multimap、Multiset</title><link>http://www.blogjava.net/hankchen/archive/2010/01/13/309257.html</link><dc:creator>hankchen</dc:creator><author>hankchen</author><pubDate>Tue, 12 Jan 2010 16:51:00 GMT</pubDate><guid>http://www.blogjava.net/hankchen/archive/2010/01/13/309257.html</guid><wfw:comment>http://www.blogjava.net/hankchen/comments/309257.html</wfw:comment><comments>http://www.blogjava.net/hankchen/archive/2010/01/13/309257.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hankchen/comments/commentRss/309257.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hankchen/services/trackbacks/309257.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">&nbsp;&nbsp;1</span><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">package</span><span style="color: #000000">&nbsp;googleCollections;<br />
</span><span style="color: #008080">&nbsp;&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.ArrayList;<br />
</span><span style="color: #008080">&nbsp;&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.Collection;<br />
</span><span style="color: #008080">&nbsp;&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.HashMap;<br />
</span><span style="color: #008080">&nbsp;&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.List;<br />
</span><span style="color: #008080">&nbsp;&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.Map;<br />
</span><span style="color: #008080">&nbsp;&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.ArrayListMultimap;<br />
</span><span style="color: #008080">&nbsp;10</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.ConcurrentHashMultiset;<br />
</span><span style="color: #008080">&nbsp;11</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.Multimap;<br />
</span><span style="color: #008080">&nbsp;12</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.Multiset;<br />
</span><span style="color: #008080">&nbsp;13</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;14</span><span style="color: #000000"><img id="Codehighlighter1_353_472_Open_Image" onclick="this.style.display='none'; Codehighlighter1_353_472_Open_Text.style.display='none'; Codehighlighter1_353_472_Closed_Image.style.display='inline'; Codehighlighter1_353_472_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_353_472_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_353_472_Closed_Text.style.display='none'; Codehighlighter1_353_472_Open_Image.style.display='inline'; Codehighlighter1_353_472_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_353_472_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_353_472_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;15</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;Copyright&nbsp;(C):&nbsp;2009<br />
</span><span style="color: #008080">&nbsp;16</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;</span><span style="color: #808080">@author</span><span style="color: #008000">&nbsp;陈新汉&nbsp;</span><span style="color: #008000; text-decoration: underline">http://www.blogjava.net/hankchen</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;17</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;</span><span style="color: #808080">@version</span><span style="color: #008000">&nbsp;创建时间：Jan&nbsp;12,&nbsp;2010&nbsp;11:55:49&nbsp;PM<br />
</span><span style="color: #008080">&nbsp;18</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;19</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;20</span><span style="color: #000000"><img id="Codehighlighter1_475_529_Open_Image" onclick="this.style.display='none'; Codehighlighter1_475_529_Open_Text.style.display='none'; Codehighlighter1_475_529_Closed_Image.style.display='inline'; Codehighlighter1_475_529_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_475_529_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_475_529_Closed_Text.style.display='none'; Codehighlighter1_475_529_Open_Image.style.display='inline'; Codehighlighter1_475_529_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_475_529_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_475_529_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;21</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;模拟测试情形：描述每个学生有多本书籍<br />
</span><span style="color: #008080">&nbsp;22</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;<br />
</span><span style="color: #008080">&nbsp;23</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;Multimap适合保存柱状图的数据<br />
</span><span style="color: #008080">&nbsp;24</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;25</span><span style="color: #000000"><img id="Codehighlighter1_565_1556_Open_Image" onclick="this.style.display='none'; Codehighlighter1_565_1556_Open_Text.style.display='none'; Codehighlighter1_565_1556_Closed_Image.style.display='inline'; Codehighlighter1_565_1556_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_565_1556_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_565_1556_Closed_Text.style.display='none'; Codehighlighter1_565_1556_Open_Image.style.display='inline'; Codehighlighter1_565_1556_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;MultiCollectionsTest&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_565_1556_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_565_1556_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;26</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">&nbsp;27</span><span style="color: #000000"><img id="Codehighlighter1_569_592_Open_Image" onclick="this.style.display='none'; Codehighlighter1_569_592_Open_Text.style.display='none'; Codehighlighter1_569_592_Closed_Image.style.display='inline'; Codehighlighter1_569_592_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_569_592_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_569_592_Closed_Text.style.display='none'; Codehighlighter1_569_592_Open_Image.style.display='inline'; Codehighlighter1_569_592_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_569_592_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_569_592_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;28</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080">@param</span><span style="color: #008000">&nbsp;args<br />
</span><span style="color: #008080">&nbsp;29</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;30</span><span style="color: #000000"><img id="Codehighlighter1_634_1553_Open_Image" onclick="this.style.display='none'; Codehighlighter1_634_1553_Open_Text.style.display='none'; Codehighlighter1_634_1553_Closed_Image.style.display='inline'; Codehighlighter1_634_1553_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_634_1553_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_634_1553_Closed_Text.style.display='none'; Codehighlighter1_634_1553_Open_Image.style.display='inline'; Codehighlighter1_634_1553_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_634_1553_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_634_1553_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;31</span><span style="color: #000000"><img id="Codehighlighter1_638_657_Open_Image" onclick="this.style.display='none'; Codehighlighter1_638_657_Open_Text.style.display='none'; Codehighlighter1_638_657_Closed_Image.style.display='inline'; Codehighlighter1_638_657_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_638_657_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_638_657_Closed_Text.style.display='none'; Codehighlighter1_638_657_Open_Image.style.display='inline'; Codehighlighter1_638_657_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_638_657_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_638_657_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;32</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;以前的方式<br />
</span><span style="color: #008080">&nbsp;33</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;34</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map</span><span style="color: #000000">&lt;</span><span style="color: #000000">Student,&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">&nbsp;studentBook&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;HashMap</span><span style="color: #000000">&lt;</span><span style="color: #000000">Student,&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;&gt;</span><span style="color: #000000">();<br />
</span><span style="color: #008080">&nbsp;35</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Student&nbsp;me</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Student(</span><span style="color: #000000">"</span><span style="color: #000000">chenxinhan</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">&nbsp;36</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;books</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;ArrayList</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;</span><span style="color: #000000">();<br />
</span><span style="color: #008080">&nbsp;37</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;books.add(</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Book(</span><span style="color: #000000">"</span><span style="color: #000000">语文</span><span style="color: #000000">"</span><span style="color: #000000">));<br />
</span><span style="color: #008080">&nbsp;38</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;books.add(</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Book(</span><span style="color: #000000">"</span><span style="color: #000000">数学</span><span style="color: #000000">"</span><span style="color: #000000">));<br />
</span><span style="color: #008080">&nbsp;39</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;studentBook.put(me,books);<br />
</span><span style="color: #008080">&nbsp;40</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">遍历</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;41</span><span style="color: #008000"><img id="Codehighlighter1_931_971_Open_Image" onclick="this.style.display='none'; Codehighlighter1_931_971_Open_Text.style.display='none'; Codehighlighter1_931_971_Closed_Image.style.display='inline'; Codehighlighter1_931_971_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_931_971_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_931_971_Closed_Text.style.display='none'; Codehighlighter1_931_971_Open_Image.style.display='inline'; Codehighlighter1_931_971_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(Book&nbsp;b:books)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_931_971_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_931_971_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;42</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(b.getName());<br />
</span><span style="color: #008080">&nbsp;43</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;44</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;45</span><span style="color: #000000"><img id="Codehighlighter1_978_997_Open_Image" onclick="this.style.display='none'; Codehighlighter1_978_997_Open_Text.style.display='none'; Codehighlighter1_978_997_Closed_Image.style.display='inline'; Codehighlighter1_978_997_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_978_997_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_978_997_Closed_Text.style.display='none'; Codehighlighter1_978_997_Open_Image.style.display='inline'; Codehighlighter1_978_997_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_978_997_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_978_997_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;46</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;现在的方式<br />
</span><span style="color: #008080">&nbsp;47</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;48</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Multimap&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">Student,Book</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;newStudentBook&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;ArrayListMultimap.create();<br />
</span><span style="color: #008080">&nbsp;49</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Student&nbsp;cxh</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Student(</span><span style="color: #000000">"</span><span style="color: #000000">chenxinhan</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">&nbsp;50</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newStudentBook.put(cxh,</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Book(</span><span style="color: #000000">"</span><span style="color: #000000">语文</span><span style="color: #000000">"</span><span style="color: #000000">));<br />
</span><span style="color: #008080">&nbsp;51</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newStudentBook.put(cxh,</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Book(</span><span style="color: #000000">"</span><span style="color: #000000">数学</span><span style="color: #000000">"</span><span style="color: #000000">));<br />
</span><span style="color: #008080">&nbsp;52</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">遍历</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;53</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collection</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;list</span><span style="color: #000000">=</span><span style="color: #000000">newStudentBook.get(cxh);<br />
</span><span style="color: #008080">&nbsp;54</span><span style="color: #000000"><img id="Codehighlighter1_1269_1309_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1269_1309_Open_Text.style.display='none'; Codehighlighter1_1269_1309_Closed_Image.style.display='inline'; Codehighlighter1_1269_1309_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1269_1309_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1269_1309_Closed_Text.style.display='none'; Codehighlighter1_1269_1309_Open_Image.style.display='inline'; Codehighlighter1_1269_1309_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(Book&nbsp;b:list)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1269_1309_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1269_1309_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;55</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(b.getName());<br />
</span><span style="color: #008080">&nbsp;56</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;57</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;58</span><span style="color: #000000"><img id="Codehighlighter1_1316_1371_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1316_1371_Open_Text.style.display='none'; Codehighlighter1_1316_1371_Closed_Image.style.display='inline'; Codehighlighter1_1316_1371_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1316_1371_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1316_1371_Closed_Text.style.display='none'; Codehighlighter1_1316_1371_Open_Image.style.display='inline'; Codehighlighter1_1316_1371_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1316_1371_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_1316_1371_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">&nbsp;59</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;Multiset测试<br />
</span><span style="color: #008080">&nbsp;60</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;不同于一般的Set，Multiset可以允许重复值<br />
</span><span style="color: #008080">&nbsp;61</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;62</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Multiset</span><span style="color: #000000">&lt;</span><span style="color: #000000">Book</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;bs</span><span style="color: #000000">=</span><span style="color: #000000">ConcurrentHashMultiset.create();<br />
</span><span style="color: #008080">&nbsp;63</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Book&nbsp;b</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;Book(</span><span style="color: #000000">"</span><span style="color: #000000">Test</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">&nbsp;64</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bs.add(b);<br />
</span><span style="color: #008080">&nbsp;65</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bs.add(b);<br />
</span><span style="color: #008080">&nbsp;66</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bs.add(b);<br />
</span><span style="color: #008080">&nbsp;67</span><span style="color: #000000"><img id="Codehighlighter1_1509_1550_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1509_1550_Open_Text.style.display='none'; Codehighlighter1_1509_1550_Closed_Image.style.display='inline'; Codehighlighter1_1509_1550_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1509_1550_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1509_1550_Closed_Text.style.display='none'; Codehighlighter1_1509_1550_Open_Image.style.display='inline'; Codehighlighter1_1509_1550_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(Book&nbsp;ab:bs)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1509_1550_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1509_1550_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;68</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(ab.getName());<br />
</span><span style="color: #008080">&nbsp;69</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;70</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;71</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">&nbsp;72</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;73</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;74</span><span style="color: #000000"><img id="Codehighlighter1_1572_1758_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1572_1758_Open_Text.style.display='none'; Codehighlighter1_1572_1758_Closed_Image.style.display='inline'; Codehighlighter1_1572_1758_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1572_1758_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1572_1758_Closed_Text.style.display='none'; Codehighlighter1_1572_1758_Open_Image.style.display='inline'; Codehighlighter1_1572_1758_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Student</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1572_1758_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1572_1758_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;75</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;String&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;76</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;77</span><span style="color: #000000"><img id="Codehighlighter1_1623_1641_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1623_1641_Open_Text.style.display='none'; Codehighlighter1_1623_1641_Closed_Image.style.display='inline'; Codehighlighter1_1623_1641_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1623_1641_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1623_1641_Closed_Text.style.display='none'; Codehighlighter1_1623_1641_Open_Image.style.display='inline'; Codehighlighter1_1623_1641_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;String&nbsp;getName()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1623_1641_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1623_1641_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;78</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;79</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;80</span><span style="color: #000000"><img id="Codehighlighter1_1677_1700_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1677_1700_Open_Text.style.display='none'; Codehighlighter1_1677_1700_Closed_Image.style.display='inline'; Codehighlighter1_1677_1700_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1677_1700_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1677_1700_Closed_Text.style.display='none'; Codehighlighter1_1677_1700_Open_Image.style.display='inline'; Codehighlighter1_1677_1700_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;setName(String&nbsp;name)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1677_1700_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1677_1700_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;81</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">.name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;82</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;83</span><span style="color: #000000"><img id="Codehighlighter1_1731_1754_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1731_1754_Open_Text.style.display='none'; Codehighlighter1_1731_1754_Closed_Image.style.display='inline'; Codehighlighter1_1731_1754_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1731_1754_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1731_1754_Closed_Text.style.display='none'; Codehighlighter1_1731_1754_Open_Image.style.display='inline'; Codehighlighter1_1731_1754_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;Student(String&nbsp;name)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1731_1754_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1731_1754_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;84</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">.name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;85</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;86</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">&nbsp;87</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;88</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;89</span><span style="color: #000000"><img id="Codehighlighter1_1771_1953_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1771_1953_Open_Text.style.display='none'; Codehighlighter1_1771_1953_Closed_Image.style.display='inline'; Codehighlighter1_1771_1953_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1771_1953_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1771_1953_Closed_Text.style.display='none'; Codehighlighter1_1771_1953_Open_Image.style.display='inline'; Codehighlighter1_1771_1953_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;Book</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1771_1953_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1771_1953_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;90</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">private</span><span style="color: #000000">&nbsp;String&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;91</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">&nbsp;92</span><span style="color: #000000"><img id="Codehighlighter1_1821_1839_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1821_1839_Open_Text.style.display='none'; Codehighlighter1_1821_1839_Closed_Image.style.display='inline'; Codehighlighter1_1821_1839_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1821_1839_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1821_1839_Closed_Text.style.display='none'; Codehighlighter1_1821_1839_Open_Image.style.display='inline'; Codehighlighter1_1821_1839_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;String&nbsp;getName()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1821_1839_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1821_1839_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;93</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;94</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;95</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">&nbsp;96</span><span style="color: #000000"><img id="Codehighlighter1_1876_1899_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1876_1899_Open_Text.style.display='none'; Codehighlighter1_1876_1899_Closed_Image.style.display='inline'; Codehighlighter1_1876_1899_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1876_1899_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1876_1899_Closed_Text.style.display='none'; Codehighlighter1_1876_1899_Open_Image.style.display='inline'; Codehighlighter1_1876_1899_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;setName(String&nbsp;name)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1876_1899_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1876_1899_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">&nbsp;97</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">.name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">&nbsp;98</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">&nbsp;99</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">100</span><span style="color: #000000"><img id="Codehighlighter1_1928_1951_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1928_1951_Open_Text.style.display='none'; Codehighlighter1_1928_1951_Closed_Image.style.display='inline'; Codehighlighter1_1928_1951_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1928_1951_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1928_1951_Closed_Text.style.display='none'; Codehighlighter1_1928_1951_Open_Image.style.display='inline'; Codehighlighter1_1928_1951_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;Book(String&nbsp;name)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1928_1951_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1928_1951_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">101</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">this</span><span style="color: #000000">.name&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;name;<br />
</span><span style="color: #008080">102</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">103</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">104</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span></div>
<br />
（友情提示：本博文章欢迎转载，但请注明出处：hankchen，<a title="http://www.blogjava.net/hankchen" href="http://www.blogjava.net/hankchen"><strong>http://www.blogjava.net/hankchen</strong></a><strong>）</strong><br />
<br />
<img src ="http://www.blogjava.net/hankchen/aggbug/309257.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hankchen/" target="_blank">hankchen</a> 2010-01-13 00:51 <a href="http://www.blogjava.net/hankchen/archive/2010/01/13/309257.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Google Collections之Immutable类</title><link>http://www.blogjava.net/hankchen/archive/2010/01/13/309255.html</link><dc:creator>hankchen</dc:creator><author>hankchen</author><pubDate>Tue, 12 Jan 2010 16:47:00 GMT</pubDate><guid>http://www.blogjava.net/hankchen/archive/2010/01/13/309255.html</guid><wfw:comment>http://www.blogjava.net/hankchen/comments/309255.html</wfw:comment><comments>http://www.blogjava.net/hankchen/archive/2010/01/13/309255.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hankchen/comments/commentRss/309255.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hankchen/services/trackbacks/309255.html</trackback:ping><description><![CDATA[<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><span style="color: #008080">&nbsp;1</span><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><span style="color: #0000ff">package</span><span style="color: #000000">&nbsp;googleCollections;<br />
</span><span style="color: #008080">&nbsp;2</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.ArrayList;<br />
</span><span style="color: #008080">&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.Arrays;<br />
</span><span style="color: #008080">&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.Collections;<br />
</span><span style="color: #008080">&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.List;<br />
</span><span style="color: #008080">&nbsp;7</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;java.util.Map.Entry;<br />
</span><span style="color: #008080">&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.ImmutableList;<br />
</span><span style="color: #008080">10</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.ImmutableMap;<br />
</span><span style="color: #008080">11</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">import</span><span style="color: #000000">&nbsp;com.google.common.collect.ImmutableSet;<br />
</span><span style="color: #008080">12</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">13</span><span style="color: #000000"><img id="Codehighlighter1_306_393_Open_Image" onclick="this.style.display='none'; Codehighlighter1_306_393_Open_Text.style.display='none'; Codehighlighter1_306_393_Closed_Image.style.display='inline'; Codehighlighter1_306_393_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_306_393_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_306_393_Closed_Text.style.display='none'; Codehighlighter1_306_393_Open_Image.style.display='inline'; Codehighlighter1_306_393_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_306_393_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_306_393_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">14</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;Copyright&nbsp;(C):&nbsp;2009<br />
</span><span style="color: #008080">15</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;</span><span style="color: #808080">@author</span><span style="color: #008000">&nbsp;陈新汉&nbsp;<br />
</span><span style="color: #008080">16</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;</span><span style="color: #808080">@version</span><span style="color: #008000">&nbsp;创建时间：Jan&nbsp;12,&nbsp;2010&nbsp;11:10:05&nbsp;PM<br />
</span><span style="color: #008080">17</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">18</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /><br />
</span><span style="color: #008080">19</span><span style="color: #000000"><img id="Codehighlighter1_396_522_Open_Image" onclick="this.style.display='none'; Codehighlighter1_396_522_Open_Text.style.display='none'; Codehighlighter1_396_522_Closed_Image.style.display='inline'; Codehighlighter1_396_522_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_396_522_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_396_522_Closed_Text.style.display='none'; Codehighlighter1_396_522_Open_Image.style.display='inline'; Codehighlighter1_396_522_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_396_522_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_396_522_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">20</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;不可变集合类<br />
</span><span style="color: #008080">21</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;适合作为内容数据不可变的容器类<br />
</span><span style="color: #008080">22</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;例如：常量数组、常量Map等等。<br />
</span><span style="color: #008080">23</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;<br />
</span><span style="color: #008080">24</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;*&nbsp;注意：为了实现不可变集合，JDK&nbsp;5.0里面是通过Collections.unmodifiableList(list)实现的！<br />
</span><span style="color: #008080">25</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">26</span><span style="color: #000000"><img id="Codehighlighter1_555_1600_Open_Image" onclick="this.style.display='none'; Codehighlighter1_555_1600_Open_Text.style.display='none'; Codehighlighter1_555_1600_Closed_Image.style.display='inline'; Codehighlighter1_555_1600_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" /><img style="display: none" id="Codehighlighter1_555_1600_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_555_1600_Closed_Text.style.display='none'; Codehighlighter1_555_1600_Open_Image.style.display='inline'; Codehighlighter1_555_1600_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;ImmutableListTest&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_555_1600_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_555_1600_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">27</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">28</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">final</span><span style="color: #000000">&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;imlist;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">以前实现（方式一）</span><span style="color: #008000"><br />
</span><span style="color: #008080">29</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">final</span><span style="color: #000000">&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;imlist2;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">以前实现（方式二）</span><span style="color: #008000"><br />
</span><span style="color: #008080">30</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">31</span><span style="color: #000000"><img id="Codehighlighter1_677_946_Open_Image" onclick="this.style.display='none'; Codehighlighter1_677_946_Open_Text.style.display='none'; Codehighlighter1_677_946_Closed_Image.style.display='inline'; Codehighlighter1_677_946_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_677_946_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_677_946_Closed_Text.style.display='none'; Codehighlighter1_677_946_Open_Image.style.display='inline'; Codehighlighter1_677_946_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_677_946_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_677_946_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">32</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;list</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;ArrayList</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">();<br />
</span><span style="color: #008080">33</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(</span><span style="color: #000000">"</span><span style="color: #000000">a</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">34</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(</span><span style="color: #000000">"</span><span style="color: #000000">b</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">35</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.add(</span><span style="color: #000000">"</span><span style="color: #000000">c</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">36</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imlist</span><span style="color: #000000">=</span><span style="color: #000000">Collections.unmodifiableList(list);&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">通过这种方式实现不可变的集合,以前实现（方式一）</span><span style="color: #008000"><br />
</span><span style="color: #008080">37</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imlist2</span><span style="color: #000000">=</span><span style="color: #000000">Collections.unmodifiableList(Arrays.asList(</span><span style="color: #000000">"</span><span style="color: #000000">a</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">b</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">c</span><span style="color: #000000">"</span><span style="color: #000000">));&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">或者，通过这种方式实现不可变的集合,以前实现（方式二）</span><span style="color: #008000"><br />
</span><span style="color: #008080">38</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">39</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">40</span><span style="color: #000000"><img id="Codehighlighter1_950_973_Open_Image" onclick="this.style.display='none'; Codehighlighter1_950_973_Open_Text.style.display='none'; Codehighlighter1_950_973_Closed_Image.style.display='inline'; Codehighlighter1_950_973_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_950_973_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_950_973_Closed_Text.style.display='none'; Codehighlighter1_950_973_Open_Image.style.display='inline'; Codehighlighter1_950_973_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_950_973_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_950_973_Open_Text"><span style="color: #008000">/**</span><span style="color: #008000"><br />
</span><span style="color: #008080">41</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080">@param</span><span style="color: #008000">&nbsp;args<br />
</span><span style="color: #008080">42</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">*/</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">43</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">public</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;<br />
</span><span style="color: #008080">44</span><span style="color: #000000"><img id="Codehighlighter1_1017_1597_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1017_1597_Open_Text.style.display='none'; Codehighlighter1_1017_1597_Closed_Image.style.display='inline'; Codehighlighter1_1017_1597_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1017_1597_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1017_1597_Closed_Text.style.display='none'; Codehighlighter1_1017_1597_Open_Image.style.display='inline'; Codehighlighter1_1017_1597_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1017_1597_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1017_1597_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">45</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">常量列表</span><span style="color: #008000"><br />
</span><span style="color: #008080">46</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImmutableList</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;imlist</span><span style="color: #000000">=</span><span style="color: #000000">ImmutableList.of(</span><span style="color: #000000">"</span><span style="color: #000000">a</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">b</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">c</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">47</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">imlist.add("d");&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">注意，编译会报异常</span><span style="color: #008000"><br />
</span><span style="color: #008080">48</span><span style="color: #008000"><img id="Codehighlighter1_1147_1177_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1147_1177_Open_Text.style.display='none'; Codehighlighter1_1147_1177_Closed_Image.style.display='inline'; Codehighlighter1_1147_1177_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1147_1177_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1147_1177_Closed_Text.style.display='none'; Codehighlighter1_1147_1177_Open_Image.style.display='inline'; Codehighlighter1_1147_1177_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(String&nbsp;s:imlist)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1147_1177_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1147_1177_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">49</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(s);<br />
</span><span style="color: #008080">50</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">51</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">52</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">常量Map</span><span style="color: #008000"><br />
</span><span style="color: #008080">53</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImmutableMap</span><span style="color: #000000">&lt;</span><span style="color: #000000">String,String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;immap</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000">&nbsp;ImmutableMap.Builder</span><span style="color: #000000">&lt;</span><span style="color: #000000">String,String</span><span style="color: #000000">&gt;</span><span style="color: #000000">()<br />
</span><span style="color: #008080">54</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&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;.put(</span><span style="color: #000000">"</span><span style="color: #000000">a</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">1</span><span style="color: #000000">"</span><span style="color: #000000">)<br />
</span><span style="color: #008080">55</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&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;.put(</span><span style="color: #000000">"</span><span style="color: #000000">b</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">2</span><span style="color: #000000">"</span><span style="color: #000000">)<br />
</span><span style="color: #008080">56</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&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;.put(</span><span style="color: #000000">"</span><span style="color: #000000">c</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">3</span><span style="color: #000000">"</span><span style="color: #000000">)<br />
</span><span style="color: #008080">57</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&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;.build();<br />
</span><span style="color: #008080">58</span><span style="color: #000000"><img id="Codehighlighter1_1413_1469_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1413_1469_Open_Text.style.display='none'; Codehighlighter1_1413_1469_Closed_Image.style.display='inline'; Codehighlighter1_1413_1469_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1413_1469_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1413_1469_Closed_Text.style.display='none'; Codehighlighter1_1413_1469_Open_Image.style.display='inline'; Codehighlighter1_1413_1469_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(Entry</span><span style="color: #000000">&lt;</span><span style="color: #000000">String,String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;e:immap.entrySet())</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1413_1469_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1413_1469_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">59</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(e.getKey()</span><span style="color: #000000">+</span><span style="color: #000000">"</span><span style="color: #000000">=</span><span style="color: #000000">"</span><span style="color: #000000">+</span><span style="color: #000000">e.getValue());<br />
</span><span style="color: #008080">60</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">61</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
</span><span style="color: #008080">62</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">常量Set</span><span style="color: #008000"><br />
</span><span style="color: #008080">63</span><span style="color: #008000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ImmutableSet</span><span style="color: #000000">&lt;</span><span style="color: #000000">String</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;imset</span><span style="color: #000000">=</span><span style="color: #000000">ImmutableSet.of(</span><span style="color: #000000">"</span><span style="color: #000000">a</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">b</span><span style="color: #000000">"</span><span style="color: #000000">,</span><span style="color: #000000">"</span><span style="color: #000000">c</span><span style="color: #000000">"</span><span style="color: #000000">);<br />
</span><span style="color: #008080">64</span><span style="color: #000000"><img id="Codehighlighter1_1564_1594_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1564_1594_Open_Text.style.display='none'; Codehighlighter1_1564_1594_Closed_Image.style.display='inline'; Codehighlighter1_1564_1594_Closed_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" /><img style="display: none" id="Codehighlighter1_1564_1594_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1564_1594_Closed_Text.style.display='none'; Codehighlighter1_1564_1594_Open_Image.style.display='inline'; Codehighlighter1_1564_1594_Open_Text.style.display='inline';" alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(String&nbsp;s:imset)</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1564_1594_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_1564_1594_Open_Text"><span style="color: #000000">{<br />
</span><span style="color: #008080">65</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(s);<br />
</span><span style="color: #008080">66</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">67</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">68</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br />
</span><span style="color: #008080">69</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br />
</span><span style="color: #008080">70</span><span style="color: #000000"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span></div>
<br />
（友情提示：本博文章欢迎转载，但请注明出处：hankchen，<a title="http://www.blogjava.net/hankchen" href="http://www.blogjava.net/hankchen"><strong>http://www.blogjava.net/hankchen</strong></a><strong>）</strong><br />
<br />
<img src ="http://www.blogjava.net/hankchen/aggbug/309255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hankchen/" target="_blank">hankchen</a> 2010-01-13 00:47 <a href="http://www.blogjava.net/hankchen/archive/2010/01/13/309255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>静态内部类</title><link>http://www.blogjava.net/hankchen/archive/2010/01/05/308310.html</link><dc:creator>hankchen</dc:creator><author>hankchen</author><pubDate>Tue, 05 Jan 2010 09:25:00 GMT</pubDate><guid>http://www.blogjava.net/hankchen/archive/2010/01/05/308310.html</guid><wfw:comment>http://www.blogjava.net/hankchen/comments/308310.html</wfw:comment><comments>http://www.blogjava.net/hankchen/archive/2010/01/05/308310.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hankchen/comments/commentRss/308310.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hankchen/services/trackbacks/308310.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;内部类是一个很好的工具，使用内部类可以简化代码，同时提高代码的优雅度。（友情提示：本博文章欢迎转载，但请注明出处：hankchen，http://www.blogjava.net/hankchen）下面介绍内部类和静态内部类的用法区别：&nbsp;&nbsp;1/**&nbsp;*//**&nbsp;&nbsp;2&nbsp;*&nbsp;&nbsp...&nbsp;&nbsp;<a href='http://www.blogjava.net/hankchen/archive/2010/01/05/308310.html'>阅读全文</a><img src ="http://www.blogjava.net/hankchen/aggbug/308310.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hankchen/" target="_blank">hankchen</a> 2010-01-05 17:25 <a href="http://www.blogjava.net/hankchen/archive/2010/01/05/308310.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一道Java多线程面试题的解答</title><link>http://www.blogjava.net/hankchen/archive/2009/12/29/307680.html</link><dc:creator>hankchen</dc:creator><author>hankchen</author><pubDate>Tue, 29 Dec 2009 12:58:00 GMT</pubDate><guid>http://www.blogjava.net/hankchen/archive/2009/12/29/307680.html</guid><wfw:comment>http://www.blogjava.net/hankchen/comments/307680.html</wfw:comment><comments>http://www.blogjava.net/hankchen/archive/2009/12/29/307680.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/hankchen/comments/commentRss/307680.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/hankchen/services/trackbacks/307680.html</trackback:ping><description><![CDATA[<span style="font-size: 12pt; font-family: 宋体"><br />
最近看到一道面试题，比较有意思：</span> <br />
<p><span style="font-size: 12pt; font-family: 宋体"><br />
有三个线程</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">ID</span><span style="font-size: 12pt; font-family: 宋体">分别是</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">A</span><span style="font-size: 12pt; font-family: 宋体">、</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">B</span><span style="font-size: 12pt; font-family: 宋体">、</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">C,</span><span style="font-size: 12pt; font-family: 宋体">请有多线编程实现，在屏幕上循环打印</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">10</span><span style="font-size: 12pt; font-family: 宋体">次</span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">ABCABC&#8230;</span></p>
<p><span style="font-size: 12pt; font-family: 宋体">由于线程执行的不确定性，要保证这样有序的输出，必须控制好多线程的同步。<br />
<br />
</span></p>
<p><span style="font-size: 12pt; font-family: 宋体">线程同步有两种基本方法：</span></p>
<p style="margin-left: 36pt; text-indent: -36pt"><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">（1）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">synchronized</span></p>
<p style="margin-left: 36pt; text-indent: -36pt"><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">（2）<span style="font: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp; </span></span><span style="font-size: 12pt; font-family: 'Times New Roman','serif'">wait,notify,notifyAll</span></p>
<p><span style="font-size: 12pt; font-family: 宋体">现在分别采用这两种方法来解答这道题目。</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">/**</span></p>
<p style="text-indent: 6pt; text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">*&nbsp;</span><strong><span style="font-size: 12pt; color: #7f9fbf; font-family: 'Times New Roman','serif'">@author</span></strong><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">陈新汉 <font style="background-color: #cce8cf">http://www.blogjava.net/hankchen</font></span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;*&nbsp;2009</span><span style="font-size: 12pt; color: #7f7f9f; font-family: 'Times New Roman','serif'">-</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">12</span><span style="font-size: 12pt; color: #7f7f9f; font-family: 'Times New Roman','serif'">-</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">28 </span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">下午</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">01:57:04<br />
</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;*/</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">/**</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;*</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">采用多线程技术打印</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">10</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">次</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&#8220;ABC&#8221;</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">，即</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&#8220;</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">ＡＢＣＡＢＣ</span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">...&#8221;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;* </span><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">实现方式（一）利用</span><u><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">synchronized</span></u><span style="font-size: 12pt; color: #3f5fbf; font-family: 宋体">关键字实现</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;*/</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">class</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiInterviewMultithread {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">/**</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;* </span><strong><span style="font-size: 12pt; color: #7f9fbf; font-family: 'Times New Roman','serif'">@param</span></strong><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'"> <span style="background: silver">args</span></span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: #3f5fbf; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;*/</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">static </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">void</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> main(String[] <span style="background: yellow">args</span>) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XunleiLock lock = </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiLock();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> Thread(</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">, lock)).start();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> Thread(</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"B"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">, lock)).start();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> Thread(</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">new</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"C"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">, lock)).start();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">}</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">class</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">implements</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> Runnable {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> String </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = </span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">""</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiLock </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">null</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">int </span></strong><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">=10;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter(String name, XunleiLock lock) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = name;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = lock;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 12pt; color: #646464; font-family: 'Times New Roman','serif'">@Override</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">void</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> run() {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">while</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">(</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&gt;0) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">synchronized</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.getName().equalsIgnoreCase(</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; System.</span><em><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">out</span></em><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.print(</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">--;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"B"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; } </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">else</span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"B"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"C"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; } </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">else</span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"C"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">}</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">class</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiLock </span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">{</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> String </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = </span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;String getName() {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">return </span></strong><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">void</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> setName(String name) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = name;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">}</span></p>
<p><span style="font-size: 12pt; color: black; font-family: 宋体">方法（二）</span><span style="font-size: 12pt; color: black; font-family: 宋体">线程类修改如下，其他类一样：</span></p>
<p style="text-align: left" align="left"><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">class</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter2 </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">implements</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> Runnable {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> String </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = </span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">""</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiLock </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">null</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">private </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">int </span></strong><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">=10;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> XunleiPrinter2(String name, XunleiLock lock) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = name;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> = lock;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 12pt; color: #646464; font-family: 'Times New Roman','serif'">@Override</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">public </span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">void</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> run() {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">while</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">(</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&gt;0) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">synchronized</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">while</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">(!</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.getName().equalsIgnoreCase(</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">try</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">{</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.wait();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">catch</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">(InterruptedException e){</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; e.printStackTrace();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; System.</span><em><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">out</span></em><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.print(</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">count</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">--;</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"B"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; } </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">else</span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"B"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"C"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; } </span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">else</span></strong><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">if</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'"> (</span><strong><span style="font-size: 12pt; color: #7f0055; font-family: 'Times New Roman','serif'">this</span></strong><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.</span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">name</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.equals(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"C"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">)) {</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.setName(</span><span style="font-size: 12pt; color: #2a00ff; font-family: 'Times New Roman','serif'">"A"</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">);</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&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; </span><span style="font-size: 12pt; color: #0000c0; font-family: 'Times New Roman','serif'">lock</span><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">.notifyAll();</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p style="text-align: left" align="left"><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>
<p><span style="font-size: 12pt; color: black; font-family: 'Times New Roman','serif'">}</span></p>
<span style="font-size: 12pt; font-family: 宋体">（友情提示：本博文章欢迎转载，但请注明出处：陈新汉，<a title="http://www.blogjava.net/hankchen" href="http://www.blogjava.net/hankchen">http://www.blogjava.net/hankchen</a>）<br />
</span>
<img src ="http://www.blogjava.net/hankchen/aggbug/307680.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/hankchen/" target="_blank">hankchen</a> 2009-12-29 20:58 <a href="http://www.blogjava.net/hankchen/archive/2009/12/29/307680.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>