Thread的创建方式

  • 继承Thread类与实现Runnable接口
public class ThreadDemo1 {
    public static void main(String[] args){
        new MyThread("thread1").start();
        new Thread(new MyRunnable(),"thread2").start();
    }
    //继承Thread类
    public static class MyThread extends Thread {
        public MyThread(String threadName) {
            super(threadName);
        }
        @Override
        public void run() {
            super.run();
            System.out.println(getName() + " is running...");
        }
    }
    //实现Runnable接口
    public static class MyRunnable implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " is running...");
        }
    }
}
  • 何时继承与何时实现接口

一般情况下,我们尽量使用实现接口的方式,遵循“面向接口编程”的思想,因为Java中是单继承的,因此,如果我们如果需要该类继承其他的类,那么必须选择实现接口的方式。
如果要求线程之间数据共享,则采用实现接口的方式;如果要求线程之间数据独立,则采用继承方式。

public class ThreadDemo1 {
    public static void main(String[] args){
        for (int i = 0; i < 5; i ++)
            new MyThread("extend-thread" + i).start();
        //定义一个MyRunnable,并在线程中共享
        MyRunnable runnable = new MyRunnable();
        for (int i = 0; i < 5; i ++)
            new Thread(runnable,"interface-thread" + i).start();
    }
    //继承Thread类
    public static class MyThread extends Thread {
        private int value ;
        public MyThread(String threadName) {
            super(threadName);
        }
        @Override
        public void run() {
            for (int i = 0; i < 100; i ++)
                value ++;
            System.out.println(getName() + " get value : " + value);
        }
    }
    //实现Runnable接口
    public static class MyRunnable implements Runnable{
        private int value;
        @Override
        public void run() {
            for (int i = 0; i < 100; i ++)
                value ++;
            System.out.println(Thread.currentThread().getName() + " get value : " + value);
        }
    }
}

输出结果:
extend-thread4 get value : 100
extend-thread2 get value : 100
extend-thread1 get value : 100
extend-thread0 get value : 100
extend-thread3 get value : 100
interface-thread0 get value : 200
interface-thread3 get value : 300
interface-thread1 get value : 200
interface-thread4 get value : 400
interface-thread2 get value : 500

如上,我们可以看继承方式中线程之间的数据是独立的,而实现接口的方式线程之间的数据时共享的。

  • 实现Runnable接口与实现Callable接口

Runnable是执行工作的独立任务,但是它不返回值。如果你希望任务完成后能够返回一个值,那么可以实现Callable接口而不是Runnable接口。

public class ThreadDemo1 {
    public static void main(String[] args){
        new Thread(new MyRunnable(),"runnable-thread").start();
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
        new Thread(futureTask).start();
        try {
            System.out.println("callable中返回的结果:" + futureTask.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
    //实现Runnable接口
    public static class MyRunnable implements Runnable{
        private int value;
        @Override
        public void run() {
            for (int i = 0; i < 100; i ++)
                value ++;
            System.out.println(Thread.currentThread().getName() + " get value : " + value);
        }
    }
    //实现callable接口
    public static class MyCallable implements Callable<Integer>{
        private int value;
        @Override
        public Integer call() throws Exception {
            for (int i = 0; i < 100; i ++)
                value ++;
            return value;
        }
    }
}
输出结果:
runnable-thread get value : 100
callable中返回的结果:100
  • Callable的优点:
    运行Callable任务可以拿到一个Future对象,表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。通过Future对象可以了解任务执行情况,可取消任务的执行,还可获取执行结果。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 下面是我自己收集整理的Java线程相关的面试题,可以用它来好好准备面试。 参考文档:-《Java核心技术 卷一》-...
    阿呆变Geek阅读 14,910评论 14 507
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • 原本昨天要写这篇文章,孩子吵着要睡觉。就落下了! 原本心里暗下决心,每天要写感恩日记。又落下了! 去年也是家人要休...
    金兰幸福旅程阅读 149评论 0 0
  • 熬夜伤身这个道理大家都知道,但是我们却还是经常晚睡。为此我做了一个小调查,看看大家晚睡的原因是什么。一来借鉴下不同...
    乔克儿阅读 561评论 7 5