java创建线程implement runnable 和 extends thread 比较

本文转载自: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 方式创建线程时,发生了什么

截取上面例子中的两行代码:

[java] view plain copy print ?
  1. ImplementsRunnable rc = new ImplementsRunnable();  
  2. Thread t1 = new Thread(rc);  
rc对象是一个Runnable类,把它作为参数通过new Thread(Runnable arg)初始化了一个Thread。

我们进入Thread类的源码看一下(经过简化,要查看完整方法,参看java源码):

当new Thread的时候,会调用Thread的init()方法,初始化这个线程

[java] view plain copy print ?
  1. /** 
  2.      * 初始化一个Thread 
  3.      * ....... 
  4.      * @参数 target :这个target参数的run()方法将会被最后调用(the object whose run() method gets called) 
  5.      * ........ 
  6.      */  
  7.     private void init(...,Runnable target,....) {  
  8.     ......  
  9.     this.target = target;  
  10.     ......  
  11.     }  

我们看到这个函数的关键是用传递给他的参数初始化这个Thread,注释中明确说道调用Thread的start()方法时,最终会调用的是这个target参数的run方法,当这个Thread调用start()方法后,最终会调用到如下run()方法:
[java] view plain copy print ?
  1. public void run() {  
  2. (target != null) {  
  3.  target.run();  
  4.   
  5. }  

我们可以看到正如前面init()方法的注释所描述的一样,最终会调用的是线程init()时传递给这个Thread的target参数的run方法。

所以正如一开始的例子中的代码那样,用同一个Runnable target初始化多个线程,那么这多个线程都会调用同一个target的run()方

法。至始至终这个ImplementsRunnable对象就只有一个。


在这里要加一段话 虽然这样实现了资源的共享,但是并没有实现资源的同步,如下例子,我加入一个条件,如果counter小于10才能继续+1(比如多个窗口同时卖火车票的场景):

[java] view plain copy print ?
  1. class ImplementsRunnable implements Runnable {  
  2.         private int counter = 0;  
  3.         public void run() {  
  4.             while(counter<10){  
  5.                 counter++;  
  6.                 try {  
  7.                     Thread.sleep(1000);  
  8.                 } catch (InterruptedException e) {  
  9.                 }  
  10.                 System.out.println(Thread.currentThread().getName()+":"+ counter);  
  11.             }  
  12.         }  
  13. }  

[java] view plain copy print ?
  1. public static void main(String args[]) throws Exception {  
  2.           // Multiple threads share the same object.  
  3.           ImplementsRunnable rc = new ImplementsRunnable();  
  4.           Thread t1 = new Thread(rc);  
  5.           t1.start();  
  6.           Thread.sleep(1000); // Waiting for 1 second before starting next thread  
  7.           Thread t2 = new Thread(rc);  
  8.           t2.start();  
  9.           Thread.sleep(1000); // Waiting for 1 second before starting next thread  
  10.           Thread t3 = new Thread(rc);  
  11.           t3.start();  
  12.         }  
运行结果如下,出现了同步错误:

[java] view plain copy print ?
  1. Thread-5:2  
  2. Thread-6:3  
  3. Thread-5:4  
  4. Thread-5:6  
  5. Thread-6:7  
  6. Thread-7:8  
  7. Thread-5:9  
  8. Thread-6:10  
  9. Thread-7:9  
  10. Thread-5:10  
如果我们要实现同步的话,就需要加入synchronized方式了(当然还可以通过原子操作的方式通过CAS无锁实现)

修改后的代码如下:

[java] view plain copy print ?
  1. class ImplementsRunnable implements Runnable {  
  2.         private int counter = 0;  
  3.         public void run() {  
  4.             while(counter<10){  
  5.                 synchronized(this){  
  6.                     if(counter<10){  
  7.                         counter++;  
  8.                         System.out.println(Thread.currentThread().getName()+":"+ counter);  
  9.                     }  
  10.                 }  
  11.             }  
  12.         }  
  13. }  
运行结果如下,达到了我们的目的:

[java] view plain copy print ?
  1. Thread-5:1  
  2. Thread-6:2  
  3. Thread-7:3  
  4. Thread-7:4  
  5. Thread-6:5  
  6. Thread-6:6  
  7. Thread-6:7  
  8. Thread-6:8  
  9. Thread-6:9  
  10. Thread-5:10  

那我们在看看通过extend Thread 的方式。它并没有一个共同的Runnable对象来初始化3个Thread对象。每一个Thread都拥有自己一个独立的Runnable对象。

当然不能实现资源的共享,因为现在每个Thread都有一个自己counter对象了。


那么问题来了:难道用extends Thread的方式就不能实现共享吗,答案是肯定可以实现:

方法有很多。我自己试着写了一个:

[java] view plain copy print ?
  1. package test;  
  2. public class TestThread {  
  3.   
  4.     public static void main(String args[]) throws Exception {  
  5.           ExtendsThread tc1 = new ExtendsThread();  
  6.           tc1.start();  
  7.           ExtendsThread tc2 = new ExtendsThread();  
  8.           tc2.start();  
  9.           ExtendsThread tc3 = new ExtendsThread();  
  10.           tc3.start();  
  11.         }  
  12. }  
  13.   
  14. class ExtendsThread extends Thread {  
  15.         public void run() {  
  16.             while(Resource.getCounter()<10){  
  17.                 Resource.count();  
  18.             }  
  19.         }  
  20.   
  21. }  
多加入了一个类Resource.java
[java] view plain copy print ?
  1. package test;  
  2.   
  3. public class Resource {  
  4.     private static int counter = 0;  
  5.     public static int getCounter(){  
  6.         return counter;  
  7.     }  
  8.     public static synchronized void count(){  
  9.         if(counter<10){  
  10.             counter++;  
  11.             try {  
  12.                 Thread.sleep(500);  
  13.             } catch (InterruptedException e) {  
  14.                 e.printStackTrace();  
  15.             }  
  16.             System.out.println(Thread.currentThread().getName()+":"+counter);  
  17.         }  
  18.     }  
  19. }   

运行结果如下:

[java] view plain copy print ?
  1. Thread-5:1  
  2. Thread-5:2  
  3. Thread-6:3  
  4. Thread-5:4  
  5. Thread-7:5  
  6. Thread-6:6  
  7. Thread-7:7  
  8. Thread-6:8  
  9. Thread-7:9  
  10. Thread-6:10  

可以看到 在这段代码中 我counter设置成了static,相应的用了class级别的锁。

不过相比implement runnable的方式还是麻烦了很多。


所以从上面两个比较我们可以得出

实现Runnable接口相比继承Thread类有如下好处:

  ->避免单点继承的局限,一个类可以继承多个接口。

  ->适合于资源的共享

你可能感兴趣的:(java创建线程implement runnable 和 extends thread 比较)