010:“implements Runnable” vs. “extends Thread”?

题目:“implements Runnable” vs. “extends Thread”?

具体实现方式如下程序





public class stackoverFlow {

    public static void main(String[] args) {
        new MyThread().start();
        new MyThread().start();
        new MyThread().start();

        System.out.println("#######################");
        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable).start();
        new Thread(myRunnable).start();
        new Thread(myRunnable).start();
    }

}
/**
 * 继承 Thread类实现多线程
 *
 */
class MyThread extends Thread {
    public MyThread() {
        super("MyThread");
    }
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("i = " + i +" MyThread "+Thread.currentThread().getName());
        }

    }
}

/**
 * 实现Runnable接口实现多线程
 *
 */
class MyRunnable implements Runnable{
    public void run() {
        for(int i=0;i<10;i++){
            System.out.println("i = " + i +" MyRunnable"+Thread.currentThread().getName());
        }
    }
}

测试输出

#######################
i = 0 MyThread MyThread
i = 0 MyThread MyThread
i = 1 MyThread MyThread
i = 2 MyThread MyThread
i = 3 MyThread MyThread
i = 4 MyThread MyThread
i = 5 MyThread MyThread
i = 6 MyThread MyThread
i = 7 MyThread MyThread
i = 8 MyThread MyThread
i = 9 MyThread MyThread
i = 0 MyThread MyThread
i = 1 MyThread MyThread
i = 2 MyThread MyThread
i = 3 MyThread MyThread
i = 4 MyThread MyThread
i = 5 MyThread MyThread
i = 6 MyThread MyThread
i = 7 MyThread MyThread
i = 8 MyThread MyThread
i = 9 MyThread MyThread
i = 1 MyThread MyThread
i = 2 MyThread MyThread
i = 3 MyThread MyThread
i = 4 MyThread MyThread
i = 5 MyThread MyThread
i = 6 MyThread MyThread
i = 7 MyThread MyThread
i = 8 MyThread MyThread
i = 9 MyThread MyThread
i = 0 MyRunnableThread-0
i = 0 MyRunnableThread-2
i = 1 MyRunnableThread-2
i = 2 MyRunnableThread-2
i = 3 MyRunnableThread-2
i = 4 MyRunnableThread-2
i = 5 MyRunnableThread-2
i = 6 MyRunnableThread-2
i = 7 MyRunnableThread-2
i = 8 MyRunnableThread-2
i = 0 MyRunnableThread-1
i = 1 MyRunnableThread-1
i = 2 MyRunnableThread-1
i = 3 MyRunnableThread-1
i = 4 MyRunnableThread-1
i = 5 MyRunnableThread-1
i = 6 MyRunnableThread-1
i = 7 MyRunnableThread-1
i = 8 MyRunnableThread-1
i = 9 MyRunnableThread-1
i = 1 MyRunnableThread-0
i = 9 MyRunnableThread-2
i = 2 MyRunnableThread-0
i = 3 MyRunnableThread-0
i = 4 MyRunnableThread-0
i = 5 MyRunnableThread-0
i = 6 MyRunnableThread-0
i = 7 MyRunnableThread-0
i = 8 MyRunnableThread-0
i = 9 MyRunnableThread-0

关系图
这里写图片描述

可以看出
Runnable可以避免由于java的单继承机制带来的局限。可以再继承其他类的同时,还能实现多线程的功能

本专题来源stackoverflow 标签是java的投票数比较高的问题以及回答,我只对上面的回答根据自己的理解做下总结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值