﻿<?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-think in coding-随笔分类-java</title><link>http://www.blogjava.net/skylight/category/52903.html</link><description>coding and life</description><language>zh-cn</language><lastBuildDate>Wed, 12 Dec 2012 16:29:53 GMT</lastBuildDate><pubDate>Wed, 12 Dec 2012 16:29:53 GMT</pubDate><ttl>60</ttl><item><title>java 折半查找</title><link>http://www.blogjava.net/skylight/archive/2012/11/17/391486.html</link><dc:creator>skylight</dc:creator><author>skylight</author><pubDate>Sat, 17 Nov 2012 00:30:00 GMT</pubDate><guid>http://www.blogjava.net/skylight/archive/2012/11/17/391486.html</guid><wfw:comment>http://www.blogjava.net/skylight/comments/391486.html</wfw:comment><comments>http://www.blogjava.net/skylight/archive/2012/11/17/391486.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/skylight/comments/commentRss/391486.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/skylight/services/trackbacks/391486.html</trackback:ping><description><![CDATA[
 
<div style="padding: 4px 5px 4px 4px; border: 1px solid rgb(204, 204, 204); width: 98%; font-size: 13px; word-break: break-all; background-color: rgb(238, 238, 238);"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0, 128, 128);">&nbsp;1</span><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif"  alt="" /><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;com.test2;<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;2</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;3</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_39_768_Open_Image" onclick="this.style.display='none'; Codehighlighter1_39_768_Open_Text.style.display='none'; Codehighlighter1_39_768_Closed_Image.style.display='inline'; Codehighlighter1_39_768_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none;" id="Codehighlighter1_39_768_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_39_768_Closed_Text.style.display='none'; Codehighlighter1_39_768_Open_Image.style.display='inline'; Codehighlighter1_39_768_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;Demo2&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_39_768_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_39_768_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;4</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;5</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_43_66_Open_Image" onclick="this.style.display='none'; Codehighlighter1_43_66_Open_Text.style.display='none'; Codehighlighter1_43_66_Closed_Image.style.display='inline'; Codehighlighter1_43_66_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_43_66_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_43_66_Closed_Text.style.display='none'; Codehighlighter1_43_66_Open_Image.style.display='inline'; Codehighlighter1_43_66_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_43_66_Closed_Text">/**&nbsp;*/</span><span id="Codehighlighter1_43_66_Open_Text"><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;6</span><span style="color: rgb(0, 128, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);">&nbsp;args<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;7</span><span style="color: rgb(0, 128, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;8</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;9</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_121_489_Open_Image" onclick="this.style.display='none'; Codehighlighter1_121_489_Open_Text.style.display='none'; Codehighlighter1_121_489_Closed_Image.style.display='inline'; Codehighlighter1_121_489_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_121_489_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_121_489_Closed_Text.style.display='none'; Codehighlighter1_121_489_Open_Image.style.display='inline'; Codehighlighter1_121_489_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;search(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arrays,&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;target)&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_121_489_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_121_489_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;start&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;end&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;arrays.length&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;pos;<br /></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_206_457_Open_Image" onclick="this.style.display='none'; Codehighlighter1_206_457_Open_Text.style.display='none'; Codehighlighter1_206_457_Closed_Image.style.display='inline'; Codehighlighter1_206_457_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_206_457_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_206_457_Closed_Text.style.display='none'; Codehighlighter1_206_457_Open_Image.style.display='inline'; Codehighlighter1_206_457_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">while</span><span style="color: rgb(0, 0, 0);">&nbsp;(start&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;=</span><span style="color: rgb(0, 0, 0);">&nbsp;end)&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_206_457_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_206_457_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pos&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;(start&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;end)&nbsp;</span><span style="color: rgb(0, 0, 0);">/</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_266_287_Open_Image" onclick="this.style.display='none'; Codehighlighter1_266_287_Open_Text.style.display='none'; Codehighlighter1_266_287_Closed_Image.style.display='inline'; Codehighlighter1_266_287_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_266_287_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_266_287_Closed_Text.style.display='none'; Codehighlighter1_266_287_Open_Image.style.display='inline'; Codehighlighter1_266_287_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(arrays[pos]&nbsp;</span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);">&nbsp;target)&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_266_287_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_266_287_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;pos;<br /></span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_320_381_Open_Image" onclick="this.style.display='none'; Codehighlighter1_320_381_Open_Text.style.display='none'; Codehighlighter1_320_381_Closed_Image.style.display='inline'; Codehighlighter1_320_381_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_320_381_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_320_381_Closed_Text.style.display='none'; Codehighlighter1_320_381_Open_Image.style.display='inline'; Codehighlighter1_320_381_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">&nbsp;(arrays[pos]&nbsp;</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">&nbsp;target)&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_320_381_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_320_381_Open_Text"><span style="color: rgb(0, 0, 0);">{</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;如果数组中间的数大于目标，则将end的位置变成数组中间位置－１的位置</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 128, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;pos&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">20</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_388_453_Open_Image" onclick="this.style.display='none'; Codehighlighter1_388_453_Open_Text.style.display='none'; Codehighlighter1_388_453_Closed_Image.style.display='inline'; Codehighlighter1_388_453_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_388_453_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_388_453_Closed_Text.style.display='none'; Codehighlighter1_388_453_Open_Image.style.display='inline'; Codehighlighter1_388_453_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_388_453_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_388_453_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">21</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;start&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;pos&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;如果数组中间的数小于目标，则将start的位置变成数组中间位置+１的位置</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">22</span><span style="color: rgb(0, 128, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">23</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">24</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;若没有查找到，则返回－１</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">25</span><span style="color: rgb(0, 128, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" /></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">26</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">27</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_532_765_Open_Image" onclick="this.style.display='none'; Codehighlighter1_532_765_Open_Text.style.display='none'; Codehighlighter1_532_765_Closed_Image.style.display='inline'; Codehighlighter1_532_765_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_532_765_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_532_765_Closed_Text.style.display='none'; Codehighlighter1_532_765_Open_Image.style.display='inline'; Codehighlighter1_532_765_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_532_765_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_532_765_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">28</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_551_591_Open_Image" onclick="this.style.display='none'; Codehighlighter1_551_591_Open_Text.style.display='none'; Codehighlighter1_551_591_Closed_Image.style.display='inline'; Codehighlighter1_551_591_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_551_591_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_551_591_Closed_Text.style.display='none'; Codehighlighter1_551_591_Open_Image.style.display='inline'; Codehighlighter1_551_591_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">[]&nbsp;arrays&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_551_591_Closed_Text"><img src="http://www.blogjava.net/Images/dot.gif"  alt="" /></span><span id="Codehighlighter1_551_591_Open_Text"><span style="color: rgb(0, 0, 0);">{&nbsp;</span><span style="color: rgb(0, 0, 0);">2</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">3</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">28</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">39</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">59</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">288</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">322</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">324</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">2323</span><span style="color: rgb(0, 0, 0);">&nbsp;}</span></span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">29</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(search(arrays,&nbsp;</span><span style="color: rgb(0, 0, 0);">28</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">30</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(search(arrays,&nbsp;</span><span style="color: rgb(0, 0, 0);">322</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">31</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(search(arrays,&nbsp;</span><span style="color: rgb(0, 0, 0);">59</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">32</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(search(arrays,&nbsp;</span><span style="color: rgb(0, 0, 0);">288</span><span style="color: rgb(0, 0, 0);">));<br /></span><span style="color: rgb(0, 128, 128);">33</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif"  alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">34</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif"  alt="" /><br /></span><span style="color: rgb(0, 128, 128);">35</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif"  alt="" />}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">36</span><span style="color: rgb(0, 0, 0);"><img align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif"  alt="" /></span></div><img src ="http://www.blogjava.net/skylight/aggbug/391486.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/skylight/" target="_blank">skylight</a> 2012-11-17 08:30 <a href="http://www.blogjava.net/skylight/archive/2012/11/17/391486.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>多线程</title><link>http://www.blogjava.net/skylight/archive/2012/11/16/391437.html</link><dc:creator>skylight</dc:creator><author>skylight</author><pubDate>Fri, 16 Nov 2012 04:50:00 GMT</pubDate><guid>http://www.blogjava.net/skylight/archive/2012/11/16/391437.html</guid><wfw:comment>http://www.blogjava.net/skylight/comments/391437.html</wfw:comment><comments>http://www.blogjava.net/skylight/archive/2012/11/16/391437.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/skylight/comments/commentRss/391437.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/skylight/services/trackbacks/391437.html</trackback:ping><description><![CDATA[
 
多线程的概念<p>	一、程序与进程</p><p>1、程序：一段静态的代码。</p><p>2、进程：程序的一次动态执行过程，它对应从代码加载、执行到执行完毕的一个完整过程。</p><p>3、进程也称任务，支持多个进程同时执行的OS就被称为多进程OS或多任务OS。</p><p>	二、进程与线程</p><p>	在一个程序内部也可以实现多个任务并发执行，其中每个任务称为线程。</p><p>	线程是比进程更小的执行单位，它是在一个进程中独立的控制流，即程序内部的控制流。</p><p>	特点：线程不能独立运行，必须依赖于进程，在进程中运行。</p><p>	每个程序至少有一个线程称为主线程。</p><p>	单线程：只有一条线程的进程称为单线程</p><p>	多线程：有不止一个线程的进程称为多线程</p><p>	三、开启多线程的优点和缺点</p><p>	提高界面程序响应速度。通过使用线程，可以将需要大量时间完成的流程在后台启动单独的线程完成，提高前台界面的相应速度。</p><p>	充分利用系统资源，提高效率。通过在一个程序内部同时执行多个流程，可以充分利用CPU等系统资源，从而最大限度的发挥硬件的性能。</p><p>	当程序中的线程数量比较多时，系统将花费大量的时间进行线程的切换，这反而会降低程序的执行效率。但是，相对于优势来说，劣势还是很有限的，所以现在的项目开发中，多线程编程技术得到了广泛的应用。</p><h2>	多线程实现</h2><p>	一、在实现线程编程时，首先需要让一个类具备多线程的能力，继承Thread类或实现Runnable接口的类具备多线程的能力，然后创建线程对象，调用对应的启动线程方法即可实现线程编程。</p><p>	在一个程序中可以实现多个线程，多线程编程指在同一个程序中启动了两个或两个以上的线程。</p><p>	在实际实现线程时，Java语言提供了三种实现方式：</p><p>	（1）继承Thread类</p><p>	（2）实现Runnable接口</p><p>	（3）使用Timer和TimerTask组合</p><p>	二、继承Thread线程类实现多线程<br />java.lang包中提供了一个专门的线程类（Thread），在该类中封装了许多对线程进行调度和处理的方法。如果一个类继承了Thread类，则该类就具备了多线程的能力，可以多线程的方式执行。</p><div><div id="highlighter_814011" class="syntaxhighlighter  java ie"><br /><div style="padding: 4px 5px 4px 4px; border: 1px solid rgb(204, 204, 204); width: 98%; font-size: 13px; word-break: break-all; background-color: rgb(238, 238, 238);"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0, 128, 128);">&nbsp;1</span><img id="Codehighlighter1_29_59_Open_Image" onclick="this.style.display='none'; Codehighlighter1_29_59_Open_Text.style.display='none'; Codehighlighter1_29_59_Closed_Image.style.display='inline'; Codehighlighter1_29_59_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none;" id="Codehighlighter1_29_59_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_29_59_Closed_Text.style.display='none'; Codehighlighter1_29_59_Open_Image.style.display='inline'; Codehighlighter1_29_59_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MyThread&nbsp;</span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);">&nbsp;Thread</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_29_59_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_29_59_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;2</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_48_57_Open_Image" onclick="this.style.display='none'; Codehighlighter1_48_57_Open_Text.style.display='none'; Codehighlighter1_48_57_Closed_Image.style.display='inline'; Codehighlighter1_48_57_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_48_57_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_48_57_Closed_Text.style.display='none'; Codehighlighter1_48_57_Open_Image.style.display='inline'; Codehighlighter1_48_57_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;run()</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_48_57_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_48_57_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;3</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;4</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">线程体</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;5</span><span style="color: rgb(0, 128, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" /></span><span style="color: rgb(0, 0, 0);">}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;6</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">&nbsp;7</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" />继承Thread类实现多线程。<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;8</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" />MyThread&nbsp;tt1&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MyThread&nbsp;();<br /></span><span style="color: rgb(0, 128, 128);">&nbsp;9</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">启动线程</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 128, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" /></span><span style="color: rgb(0, 0, 0);">tt1.start();<br /></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_132_224_Open_Image" onclick="this.style.display='none'; Codehighlighter1_132_224_Open_Text.style.display='none'; Codehighlighter1_132_224_Closed_Image.style.display='inline'; Codehighlighter1_132_224_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none;" id="Codehighlighter1_132_224_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_132_224_Closed_Text.style.display='none'; Codehighlighter1_132_224_Open_Image.style.display='inline'; Codehighlighter1_132_224_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif"></span><span style="color: rgb(0, 0, 255);">try</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_132_224_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_132_224_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_158_222_Open_Image" onclick="this.style.display='none'; Codehighlighter1_158_222_Open_Text.style.display='none'; Codehighlighter1_158_222_Closed_Image.style.display='inline'; Codehighlighter1_158_222_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_158_222_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_158_222_Closed_Text.style.display='none'; Codehighlighter1_158_222_Open_Image.style.display='inline'; Codehighlighter1_158_222_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">&nbsp;i&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;i&nbsp;</span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">5</span><span style="color: rgb(0, 0, 0);">;i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_158_222_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_158_222_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">延时1秒</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 128, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />Thread.sleep(</span><span style="color: rgb(0, 0, 0);">1000</span><span style="color: rgb(0, 0, 0);">);<br /></span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" />System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Main:</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;i);<br /></span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_243_244_Open_Image" onclick="this.style.display='none'; Codehighlighter1_243_244_Open_Text.style.display='none'; Codehighlighter1_243_244_Closed_Image.style.display='inline'; Codehighlighter1_243_244_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none;" id="Codehighlighter1_243_244_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_243_244_Closed_Text.style.display='none'; Codehighlighter1_243_244_Open_Image.style.display='inline'; Codehighlighter1_243_244_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif">}</span></span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">(Exception&nbsp;e)</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_243_244_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_243_244_Open_Text"><span style="color: rgb(0, 0, 0);">{}</span></span></div>注意：</div></div><p>	线程的特性：随机性，系统在执行多线程程序时只保证线程是交替执行的，至于哪个线程先执行哪个线程后执行，则无法获得保证，需要书写专门的代码才可以保证执行的顺序。</p><p>	对于同一个线程类，也可以启动多个线程</p><p>	同一个线程不能启动两次，</p><p>	当自定义线程中的run方法执行完成以后，则自定义线程自然死亡。而对于系统线程来说，只有当main方法执行结束，而且启动的其它线程都结束以后，才会结束。当系统线程执行结束以后，程序的执行才真正结束。</p><p>	三、 实现Runable接口</p><p>a) 多线程对象实现java.lang.Runnable接口并且在该类中重写Runnable接口的run方法。</p><p>b) 好处：实现Runable接口的方法避免了单继承的局限性。</p><p>	例1：使用实现Runable接口的方式实现多线程。</p><div><div id="highlighter_251637" class="syntaxhighlighter  java ie"><br /><div style="padding: 4px 5px 4px 4px; border: 1px solid rgb(204, 204, 204); width: 98%; font-size: 13px; word-break: break-all; background-color: rgb(238, 238, 238);"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: rgb(0, 128, 128);">1</span><img id="Codehighlighter1_34_79_Open_Image" onclick="this.style.display='none'; Codehighlighter1_34_79_Open_Text.style.display='none'; Codehighlighter1_34_79_Closed_Image.style.display='inline'; Codehighlighter1_34_79_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none;" id="Codehighlighter1_34_79_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_34_79_Closed_Text.style.display='none'; Codehighlighter1_34_79_Open_Image.style.display='inline'; Codehighlighter1_34_79_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif"><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;MyThread2&nbsp;</span><span style="color: rgb(0, 0, 255);">implements</span><span style="color: rgb(0, 0, 0);">&nbsp;Runable</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_34_79_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_34_79_Open_Text"><span style="color: rgb(0, 0, 0);">{<br /></span><span style="color: rgb(0, 128, 128);">2</span><span style="color: rgb(0, 0, 0);"><img id="Codehighlighter1_53_54_Open_Image" onclick="this.style.display='none'; Codehighlighter1_53_54_Open_Text.style.display='none'; Codehighlighter1_53_54_Closed_Image.style.display='inline'; Codehighlighter1_53_54_Closed_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none;" id="Codehighlighter1_53_54_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_53_54_Closed_Text.style.display='none'; Codehighlighter1_53_54_Open_Image.style.display='inline'; Codehighlighter1_53_54_Open_Text.style.display='inline';" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif"></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;run()</span><span style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);" id="Codehighlighter1_53_54_Closed_Text"><img alt="" src="http://www.blogjava.net/Images/dot.gif" /></span><span id="Codehighlighter1_53_54_Open_Text"><span style="color: rgb(0, 0, 0);">{}</span></span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">重写Runable接口中的run()方法</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">3</span><span style="color: rgb(0, 128, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" /></span><span style="color: rgb(0, 0, 0);">}</span></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" />MyThread2&nbsp;mt1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MyThread2();<br /></span><span style="color: rgb(0, 128, 128);">5</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" />Thread&nbsp;t1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Thread(mt1);<br /></span><span style="color: rgb(0, 128, 128);">6</span><span style="color: rgb(0, 0, 0);"><img alt="" align="top" src="http://www.blogjava.net/images/OutliningIndicators/None.gif" />t1.start();</span></div>线程的生命周期</div></div><p>	一、线程的生命周期</p><p>	线程是一个动态执行的过程，它也有一个从产生到死亡的过程，这就是所谓的生命周期。一个线程在它的生命周期内有5种状态：</p><p>1、新建（new Thread）</p><p>	当创建Thread类的一个实例（对象）时，此线程进入新建状态（未被启动）。<br />	例如：Thread t1=new Thread();</p><p>	2、就绪（runnable）</p><p>	线程已经被启动，正在等待被分配给CPU时间片，也就是说此时线程正在就绪队列中排队等候得到CPU资源。例如：t1.start();</p><p>	3、运行（running）</p><p>	线程获得CPU资源正在执行任务（run()方法），此时除非此线程自动放弃CPU资源或者有优先级更高的线程进入，线程将一直运行到结束。</p><p>4、死亡（dead）</p><p>	当线程执行完毕或被其它线程杀死，线程就进入死亡状态，这时线程不可能再进入就绪状态等待执行。</p><p>	自然终止：正常运行run()方法后终止</p><p>	异常终止：调用stop()方法让一个线程终止运行</p><p>5、堵塞（blocked）</p><p>	由于某种原因导致正在运行的线程让出CPU并暂停自己的执行，即进入堵塞状态。</p><p>	正在睡眠：用sleep(long t) 方法可使线程进入睡眠方式。一个睡眠着的线程在指定的时间过去可进入就绪状态。</p><p>	正在等待：调用wait()方法。（调用motify()方法回到就绪状态）</p><p>	被另一个线程所阻塞：调用suspend()方法。（调用resume()方法恢复）</p> 
 
 
 
<img src ="http://www.blogjava.net/skylight/aggbug/391437.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/skylight/" target="_blank">skylight</a> 2012-11-16 12:50 <a href="http://www.blogjava.net/skylight/archive/2012/11/16/391437.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 抽象类与接口的区别</title><link>http://www.blogjava.net/skylight/archive/2012/11/14/391297.html</link><dc:creator>skylight</dc:creator><author>skylight</author><pubDate>Wed, 14 Nov 2012 02:33:00 GMT</pubDate><guid>http://www.blogjava.net/skylight/archive/2012/11/14/391297.html</guid><wfw:comment>http://www.blogjava.net/skylight/comments/391297.html</wfw:comment><comments>http://www.blogjava.net/skylight/archive/2012/11/14/391297.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/skylight/comments/commentRss/391297.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/skylight/services/trackbacks/391297.html</trackback:ping><description><![CDATA[<div>1.接口体现的是一种规范，而抽象类体现的是一种模板式设计。<br />2.接口里只能包含抽象方法，不包含已经提供实现的方法。而抽象类则完全可以包含普通方法。<br />3.接口里不能定义静态方法，抽象类里面可以定义静态方法。<br />4.接口里只能定义静态常量属性，不能定义普通属性。而抽象类可以定义普通属性也可以定义静态常量属性。<br />5.接口不包含构造器，而抽象类可以包含构造器。构造器不是为了用来创建对象，而是子类调用这些构造器来完成抽象类的初始化工作。<br /></div><img src ="http://www.blogjava.net/skylight/aggbug/391297.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/skylight/" target="_blank">skylight</a> 2012-11-14 10:33 <a href="http://www.blogjava.net/skylight/archive/2012/11/14/391297.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>