美文网首页
Thread的创建方式

Thread的创建方式

作者: 大海孤了岛 | 来源:发表于2017-05-10 14:27 被阅读11次
  • 继承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对象可以了解任务执行情况,可取消任务的执行,还可获取执行结果。

相关文章

  • 异步和线程池

    四种方式创建线程 1.通过Thread类创建线程 Thread01 thread = new Thread01()...

  • Thread的创建方式

    继承Thread类与实现Runnable接口 何时继承与何时实现接口 一般情况下,我们尽量使用实现接口的方式,遵循...

  • 走马观花-FutureTask和Callable

    创建线程的方式 直接创建Thread 通过Thread执行Runnable 通过FutureTask和Callab...

  • Thread

    Thread 创建方式 继承Thread实现 Runnable 接口lanbda 表达式 new Thread(...

  • Concurrency-Thread

    Concurrency-Thread 线程创建 线程创建有3种方式: 继承Thread类 实现Runnable接口...

  • 多线程线程——基础API

    线程的创建 创建线程有三种方式 thread,runnable,callable。 继承Thread类 实现Run...

  • 有关【线程】二三事

    1、创建线程的方式及实现(三种方式) (1)继承Thread类创建线程:定义Thread类的子类,并重写其中的该类...

  • 7.进程、线程、同步和锁

    进程Process 创建方式1:使用Runtime 创建方式2:使用ProcessBuilder 线程Thread...

  • 细说多线程之Thread VS Runnable

    线程创建的两种方式 继承 Thread 类: 实现Runnable接口: Runnable方式可以避免Thread...

  • Java Thread Overview

    线程的创建# 创建线程有两种方式:继承Thread类,或者实现Runnable接口 继承Thread类## 也可以...

网友评论

      本文标题:Thread的创建方式

      本文链接:https://www.haomeiwen.com/subject/ypnitxtx.html