http://blog.csdn.net/zhikun518/article/details/7526298

1、通过实现Runnable接口创建线程

(1).定义一个类实现Runnable接口,重写接口中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。

(2).创建Runnable接口实现类的对象。

(3).创建一个Thread类的对象,需要封装前面Runnable接口实现类的对象。(接口可以实现多继承)

(4).调用Thread对象的start()方法,启动线程

  1. public class ThreadFromRunnable implements Runnable {
  2. //static int count = 10;
  3. public void run() {
  4. int count = 10;
  5. System.out.println("\t#"+Thread.currentThread().getName()+" got count from " + count);
  6. while(count > 0)
  7. {
  8. System.out.println("#"+Thread.currentThread().getName()+" : "+ count--);
  9. }
  10. System.out.println("#"+Thread.currentThread().getName()+" : exiting "+ count--);
  11. }
  12. public static void main(String[] args)
  13. {
  14. ThreadFromRunnable tr = new ThreadFromRunnable();
  15. Thread thread = new Thread(tr);
  16. Thread thread2 = new Thread(tr);
  17. thread.start();
  18. thread2.start();
  19. }
  20. }

output:

#Thread-0 got count from 10
#Thread-1 got count from 10
#Thread-1 : 10
#Thread-1 : 9
#Thread-1 : 8
#Thread-1 : 7
#Thread-1 : 6
#Thread-1 : 5
#Thread-1 : 4
#Thread-0 : 10
#Thread-1 : 3
#Thread-0 : 9
#Thread-1 : 2
#Thread-0 : 8
#Thread-1 : 1
#Thread-0 : 7
#Thread-1 : exiting 0
#Thread-0 : 6
#Thread-0 : 5
#Thread-0 : 4
#Thread-0 : 3
#Thread-0 : 2
#Thread-0 : 1
#Thread-0 : exiting 0

2、通过继承Thread类创建线程

(1).首先定义一个类去继承Thread父类,重写父类中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。
(2).直接创建一个ThreadDemo2类的对象,也可以利用多态性,变量声明为父类的类型。

(3).调用start方法,线程t启动,隐含的调用run()方法。

  1. public class ThreadExtendsThread extends Thread {
  2. //static int count =10;
  3. public void run()
  4. {
  5. int count =10;
  6. System.out.println("\t#"+Thread.currentThread().getName()+" got count from " + count);
  7. while(count > 0)
  8. {
  9. System.out.println("{1}quot;+this.getName()+" : "+count--);
  10. }
  11. System.out.println("{1}quot;+this.getName()+" : existing count=" + count);
  12. }
  13. public static void main(String[] args)
  14. {
  15. ThreadExtendsThread thread = new ThreadExtendsThread();
  16. ThreadExtendsThread thread2 = new ThreadExtendsThread();
  17. thread.start();
  18. thread2.start();
  19. }
  20. }

output:

#Thread-0 got count from 10
#Thread-1 got count from 10
$Thread-1 : 10
$Thread-1 : 9
$Thread-1 : 8
$Thread-1 : 7
$Thread-1 : 6
$Thread-1 : 5
$Thread-1 : 4
$Thread-1 : 3
$Thread-1 : 2
$Thread-1 : 1
$Thread-0 : 10
$Thread-1 : existing count=0
$Thread-0 : 9
$Thread-0 : 8
$Thread-0 : 7
$Thread-0 : 6
$Thread-0 : 5
$Thread-0 : 4
$Thread-0 : 3
$Thread-0 : 2
$Thread-0 : 1
$Thread-0 : existing count=0

3、两种方式的比较

首先分析两种方式的输出结果,同样是创建了两个线程,为什么结果不一样呢?

使用实现Runnable接口方式创建线程可以共享同一个目标对象(TreadDemo1 tt=new TreadDemo1();),实现了多个相同线程处理同一份资源。

然后再看一段来自JDK的解释:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

采用继承Thread类方式:
(1)优点:编写简单,如果需要访问当前线程,无需使用Thread.currentThread()方法,直接使用this,即可获得当前线程。
(2)缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类。
采用实现Runnable接口方式:
(1)优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
(2)缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。

extends Thread 与 implements Runnable 的区别的更多相关文章

  1. extend Thread 和 implements Runnable

    原文地址:extend Thread 和 implements Runnable 一个Thread的实例只能产生一个线程 or: 同一实例(Runnable实例)的多个线程 look: public ...

  2. 线程的实现方法以及区别 extends Thread、implements Runable

    /** 线程存在于进程当中,进程由系统创建. 创建新的执行线程有两种方法 注意:   线程复写run方法,然后用start()方法调用,其实就是调用的run()方法,只是如果直接启动run()方法, ...

  3. java中的线程问题(三)——继承Thread VS 实现Runnable的区别

    从java的设计来看,通过继承Thread或者实现Runnable接口来创建线程本质上没有区别,从jdk帮助文档我们可以看到Thread类本身就实现了Runnable接口,如果一定要说它们有什么区别, ...

  4. java 多线程 继承Thread和实现Runnable的区别

    1)继承Thread: public class ThreadTest extends Thread { private int count; private String name; public ...

  5. Java 中的“implements Runnable” 和“extends Thread”(转)

    知识点 “implements Runnable” 和“extends Thread”的不同 具体分析 最近在学习Android中的Handler消息传递机制时,创建新线程有两种方式:一种是实现Run ...

  6. Java 中的“implements Runnable” 和“extends Thread”

    知识点 “implements Runnable” 和“extends Thread”的不同 具体分析 最近在学习Android中的Handler消息传递机制时,创建新线程有两种方式:一种是实现Run ...

  7. Thread 和 Runnable 的区别

    在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口: Thread类是在java.lang包中定义 的.一个类只要继承了Thread类同时覆写了本类中的run ...

  8. Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别

    一. java中实现线程的方式有Thread和Runnable Thread: public class Thread1 extends Thread{ @Override public void r ...

  9. Java学习从菜鸟变大鸟之三 多线程中Thread 和Runnable的区别与运用

    多线程机制是java中的一个特点,掌握它对后面的知识的理解至关重要,是java工程师的必备知识,多线程指在单个程序中可以运行多个不同的线程执行的不同的任务,线程是一个程序内部的顺序控制流.进程是静态的 ...

随机推荐

  1. 更加清楚理解mvc结构

      更加清楚理解mvc结构 文章来源:刘俊涛的博客 地址:http://www.cnblogs.com/lovebing 欢迎关注,有问题一起学习欢迎留言.评论.

  2. python3之序列化(pickle&json&shelve)

    1.pickle模块 python持久化的存储数据: python程序运行中得到了一些字符串,列表,字典等数据,想要长久的保存下来,方便以后使用,而不是简单的放入内存中关机断电就丢失数据.python ...

  3. UE4中FString转UTF8及UTF8转FString

    FString转UTF8 FString szMsg = "test msg"; TCHAR* pSendData = szMsg.GetCharArray().GetData() ...

  4. 6、ABPZero系列教程之拼多多卖家工具 框架后台的设置

    接着上篇文章,现在去修改注册登录逻辑代码还为时过早,我们还需要到后台去设置一些配置. 管理---设置 先配置好这2项设置,邮箱配置是为了验证注册时功能是否正常,下一篇文章需要用到. 注:邮箱配置中的密 ...

  5. 第四章 Windows图形界面-上

    学习<Windows程序设计>记录 概念贴士: 1. 每个GUI应用程序至少应该创建一个窗口,称为主窗口,它作为用户与应用程序间的主界面来提供服务.大多数应用程序也直接或间接地创建其他窗口 ...

  6. nodejs 做后台的一个完整业务整理

    大家知道js现在不仅仅可以写前端界面而且可以写后端的业务了,这样js就可以写一个全栈的项目.这里介绍一个nodejs + express + mongodb + bootstap 的全栈项目. 1.安 ...

  7. eclipse安装java web插件

    1 查看eclipse版本 找到eclipse的安装目录,找到readme文件,打开其中的html文件,我的是4.6版本的,代号是oxygen 2 安装 打开eclipse,点击help-Instal ...

  8. MySQL常用查询语句汇总(不定时更新)

    在这篇文章中我会通过一些例子来介绍日常编程中常用的SQL语句   目录: ## 1.数据库的建立     ## 1.数据库的建立   实例将ER图的形式给出:   由此转换的4个关系模式:      ...

  9. [翻译].NET Shell Extensions - Shell Context Menus---.net 外壳扩展-右键菜单

    我自己的前言说明: 本文原作者为    Dave Kerr,原文链接为.NET Shell Extensions - Shell Context Menus:,我是在为了完成最新需求的时候查询资料的时 ...

  10. CTF---密码学入门第五题 传统知识+古典密码

    传统知识+古典密码分值:10 来源: 霜羽 难度:易 参与人数:2297人 Get Flag:735人 答题人数:938人 解题通过率:78% 小明某一天收到一封密信,信中写了几个不同的年份     ...