本文转载自:http://blog.csdn.net/great_smile/article/details/42297077
我们都知道java实现线程有两种方法
一。继承Thread类
二。实现Runnable接口
看到很多说法他们之间有一个区别是:实现Runnable接口才能实现资源共享。继承thread的方式不行
并且附有类似以下的实例:
<code style="margin:0px; padding:0px; border:0px; vertical-align:baseline; font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif; white-space:inherit"><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">//Implement Runnable 的方式来实现线程</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">class</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">implements</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Runnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">private</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">int</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">0</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">;</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">public</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">void</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> run</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">()</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">++;</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">System</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">out</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">println</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="str" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">"ImplementsRunnable : Counter : "</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">+</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">//通过继承 Thread 类的方式</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">class</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">extends</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">private</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">int</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">0</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">;</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">public</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">void</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> run</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">()</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">++;</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">System</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">out</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">println</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="str" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">"ExtendsThread : Counter : "</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">+</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> counter</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">public</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">class</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ThreadVsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">public</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">static</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">void</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> main</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">String</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> args</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">[])</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">throws</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Exception</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">{</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">// 多线程共享同一个变量(rc)</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> rc </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t1 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">rc</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t1</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">sleep</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1000</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">//启动下一个线程前,等待一秒钟</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t2 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">rc</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t2</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">sleep</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1000</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">//<span style="white-space:inherit; line-height:17.8048px; background-color:transparent">启动下一个线程前,等待一秒钟</span></span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t3 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">rc</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> t3</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="com" style="color:#808080;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">// 通过extend Thread只能为每一个线程创建新的对象</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc1 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc1</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">sleep</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1000</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc2 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc2</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Thread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">sleep</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">(</span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1000</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">);</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc3 </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">=</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="kwd" style="color:#008b;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">new</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> tc3</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">.</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">start</span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">();</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">}</span></code>
输出结果如下:
<code style="margin:0px; padding:0px; border:0px; vertical-align:baseline; font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif; white-space:inherit"><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">2</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ImplementsRunnable</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">3</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">ExtendsThread</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="typ" style="color:#2b91af;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">Counter</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="pun" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">:</span><span class="pln" style="margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent"> </span><span class="lit" style="color:#8000;margin:0px; padding:0px; border:0px; vertical-align:baseline; background:transparent">1</span></code>
他们通过这个例子得出:实现Runnable接口才能实现资源共享。继承thread的方式不行 这个结论
其实我们仔细看一下这个例子,就会发现这个结论是错的:
1.首先我们看一下通过 Implements Runnable 方式创建线程时,发生了什么
截取上面例子中的两行代码:
我们进入Thread类的源码看一下(经过简化,要查看完整方法,参看java源码):
当new Thread的时候,会调用Thread的init()方法,初始化这个线程
我们可以看到正如前面init()方法的注释所描述的一样,最终会调用的是线程init()时传递给这个Thread的target参数的run方法。
所以正如一开始的例子中的代码那样,用同一个Runnable target初始化多个线程,那么这多个线程都会调用同一个target的run()方
法。至始至终这个ImplementsRunnable对象就只有一个。
在这里要加一段话 虽然这样实现了资源的共享,但是并没有实现资源的同步,如下例子,我加入一个条件,如果counter小于10才能继续+1(比如多个窗口同时卖火车票的场景):
修改后的代码如下:
那我们在看看通过extend Thread 的方式。它并没有一个共同的Runnable对象来初始化3个Thread对象。每一个Thread都拥有自己一个独立的Runnable对象。
当然不能实现资源的共享,因为现在每个Thread都有一个自己counter对象了。
那么问题来了:难道用extends Thread的方式就不能实现共享吗,答案是肯定可以实现:
方法有很多。我自己试着写了一个:
运行结果如下:
可以看到 在这段代码中 我counter设置成了static,相应的用了class级别的锁。
不过相比implement runnable的方式还是麻烦了很多。
所以从上面两个比较我们可以得出
实现Runnable接口相比继承Thread类有如下好处:
->避免单点继承的局限,一个类可以继承多个接口。
->适合于资源的共享