Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6553d7f

Browse files
committed
新增"实现Runnable接口 VS. 继承Thread类 "
1 parent 0dd7bc9 commit 6553d7f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ stackoverflow-Java-top-qa
5858
* [Java 源码里的设计模式](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Examples_of_GoF_Design_Patterns_in_Java's_core_libraries.md)
5959
* [如何产生一个随机的字母数字串作为 session 的唯一标识符](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_generate_a_random_alpha-numeric_string.md)
6060
* [如何创建单例](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What_is_an_efficient_way_to_implement_a_singleton_in_Java.md)
61+
* [实现Runnable接口 VS. 继承Thread类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/implements-runnable-vs-extends-thread.md)
6162

6263
> 网络
6364
@@ -86,7 +87,7 @@ stackoverflow-Java-top-qa
8687
- [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven)
8788
- [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed)
8889
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
89-
- ["implements Runnable" vs. "extends Thread"](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread)
90+
- [Convert a String to an enum in Java](http://stackoverflow.com/questions/604424/convert-a-string-to-an-enum-in-java)
9091
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
9192
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
9293
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##实现Runnable接口 VS. 继承Thread类 ?
2+
3+
在Java中,并发执行任务一般有两种方式:
4+
(1)实现Runnable接口
5+
(2)继承Thread类
6+
7+
一般而言,推荐使用方式(1),主要是由于大多数情况下,人们并不会特别去关注线程的行为,也不会去改写Thread已有的行为或方法,仅仅是期望执行任务而已。
8+
因此,使用接口的方式能避免引入一些并不需要的东西,同时也不会影响继承其他类,并使程序更加灵活。
9+
10+
11+
###额外的tips:
12+
(1)Runnable与Thread不是对等的概念
13+
在Thinking in Java中,作者吐槽过Runnable的命名,称其叫做Task更为合理。
14+
在Java中,Runnable只是一段用于描述任务的代码段而已,是静态的概念,需要通过线程来执行。而Thread更像是一个活体,自身就具有很多行为,能够用来执行任务。
15+
16+
(2)仅仅当你确实想要重写(override)一些已有行为时,才使用继承,否则请使用接口。
17+
18+
(3)在Java 5之前,创建了Thread却没调用其start()方法,可能导致内存泄露。
19+
20+
21+
stackoverflow链接:
22+
http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread
23+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /