java线程的实现方式

java有两种实现线程的方式,一种是通过继承Thread类,一种是通过实现Runnable接口。

通过Thread实现代码

public class A extends Thread{
    
    @Override
    public void run(){
        System.out.println("hello world");
    }
}

通过Runnable实现代码

public class B implements Runnable{
    
    @Override
    public void run(){
        System.out.println("hello world");
    }
}

相对于Thread类实现,Runnable接口实现有更好的扩张性,更加推荐使用。在java中每个类都只能有一个父类,但是却可以有很多个接口。对于一个类来说,如果选择以继承Thread方式实现线程的话,该类就不能继承其他类的,这样就使得这个类失去了通过继承获取其他类功能的能力,除非他继承的父类已经继承了Thread类(但是,可能增加了项目的复杂度,形成多级继承的架构)。如果选择Runnable接口的话,就没有那么多烦恼了,只是令某个类增加了线程的功能而已,对这个类的其他功能,以及类之间的关系都不会造成影响。稍微形象些,用游戏比较的话,选择继承Thread相当于选择了种族,选择Runnable接口则相当于拿了一件装备,所以明显是继承的影响比较大,使用Runnable接口比较靠谱。

上网查资料时,在部分文章里面提出Runnable可以实现“资源共享”,而Thread则不行。但是实际操作后,发现这是错误的。Thread类型也是继承了Runnable方法,所以也可以实现资源共享。

验证代码如下:

Runnable接口实现

public class ThreadTester {
    public static void main(String[] args){
        ThreadA a1 = new ThreadA();
        new Thread(a1,"接口实现——甲").start();
        new Thread(a1,"接口实现——乙").start();
        new Thread(a1,"接口实现——丙").start();
    }
}

class ThreadA implements Runnable{
    int i=0;
    @Override
    public void run(){
        for(;i<10;) {
            System.out.println(Thread.currentThread().getName()+" Thread is running , i is "+i++);
        }
    }
}

运行结果为:

接口实现——甲 Thread is running , i is 0
接口实现——丙 Thread is running , i is 2
接口实现——乙 Thread is running , i is 1
接口实现——乙 Thread is running , i is 5
接口实现——乙 Thread is running , i is 6
接口实现——乙 Thread is running , i is 7
接口实现——乙 Thread is running , i is 8
接口实现——乙 Thread is running , i is 9
接口实现——丙 Thread is running , i is 4
接口实现——甲 Thread is running , i is 3

Thread继承实现

public class ThreadTester {
    public static void main(String[] args){
        
        ThreadB b1 = new ThreadB();
        new Thread(b1,"类继承实现——甲").start();
        new Thread(b1,"类继承实现——乙").start();
        new Thread(b1,"类继承实现——丙").start();
    }
}

class ThreadB extends Thread{
    int i=0;
    @Override
    public void run(){
        for(;i<10;) {
            System.out.println(Thread.currentThread().getName()+" Thread is running , i is "+i++);
        }
    }
}

运行结果为:

类继承实现——甲 Thread is running , i is 0
类继承实现——甲 Thread is running , i is 2
类继承实现——甲 Thread is running , i is 3
类继承实现——甲 Thread is running , i is 4
类继承实现——乙 Thread is running , i is 1
类继承实现——乙 Thread is running , i is 7
类继承实现——乙 Thread is running , i is 8
类继承实现——甲 Thread is running , i is 6
类继承实现——丙 Thread is running , i is 5
类继承实现——乙 Thread is running , i is 9

================================

Runnable 实现资源共享的原理其实也比较简单,在以上的代码中,系统声明了一个Thread对象,并将这个对象作为一个参数传递给三个线程,然后启动。在这个过程中三个线程实际引用了一开始声明的Thread对象,使用的是这个对象的内存空间,所以任意一方的修改,就会导致其他线程也发生更改。

所以能不能资源共享,就是看是不是声明的对个对象,开辟了不同的内存空间

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值