Thread的创建方式

2017-05-10  本文已影响11人  大海孤了岛
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接口。

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
上一篇 下一篇

猜你喜欢

热点阅读