java线程的3种创建方式及优缺点

线程创建简介

  1.在java中表示线程的是Thread类。无论是那种方式创建线程,本质上都是创建Thread类的对象。
aaa
  2.Thread类继承Runnable接口,且也有以Runnable作为参数的构造方法。
aaa
  3.FutureTask这个类也实现了Runnable接口,故也可以作为参数传进Thread 里面来创建线程。

线程的创建

继承Thread类

package thread;

/**
 * 继承thread类来创建线程
 * 
 * @author dh
 *
 */
public class ExtendsThread extends Thread {

    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "    _"+i);
        }
    }

}
### 实现Runnable接口
package thread;

/**
 * 实现 Runnable接口
 * 
 * @author dh
 *
 */
public class ImplementRunnable implements Runnable {
    private int i;

    public ImplementRunnable(int i) {
        this.i = i;
    }

    @Override
    public void run() {

        for (; i < 20; i++) {
            System.out.println(Thread.currentThread().getName() + "   _" + i);
        }
    }

}
### 实现Callable接口
package thread;

import java.util.concurrent.Callable;

/**
 * 
 * @author dh
 *
 */
public class ImplementCallable implements Callable {

    private int i;

    public ImplementCallable(int i) {
        this.i = i;
    }

    @Override
    public String call() throws Exception {
        for (; i < 20; i++) {
            System.out.println(Thread.currentThread().getName() + "   _" + i);

        }
        return "1";
    }

}
### 创建线程
package thread;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
 * 
 * @author dh
 *
 */
public class TestD {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExtendsThread et = new ExtendsThread();
        et.run();



        ImplementRunnable imr = new ImplementRunnable(0);
        Thread thread = new Thread(imr);
        thread.start();
        Thread thread1 = new Thread(imr);
        thread1.start();


        ImplementCallable implCallable = new ImplementCallable(1);
        FutureTask ft = new FutureTask(implCallable);
        Thread thread2 = new Thread(ft);
        thread2.start();
        System.out.println(ft.get());

    }

}
}

结果如下:

main    _0
main    _1
main    _2
main    _3
main    _4
main    _5
main    _6
main    _7
main    _8
main    _9
Thread-1   _0
Thread-2   _0
Thread-1   _1
Thread-2   _2
Thread-1   _3
Thread-2   _4
Thread-1   _5
Thread-2   _6
Thread-1   _7
Thread-2   _8
Thread-1   _9
Thread-2   _10
Thread-1   _11
Thread-2   _12
Thread-1   _13
Thread-2   _14
Thread-1   _15
Thread-2   _16
Thread-1   _17
Thread-2   _18
Thread-1   _19
Thread-3   _1
Thread-3   _2
Thread-3   _3
Thread-3   _4
Thread-3   _5
Thread-3   _6
Thread-3   _7
Thread-3   _8
Thread-3   _9
Thread-3   _10
Thread-3   _11
Thread-3   _12
Thread-3   _13
Thread-3   _14
Thread-3   _15
Thread-3   _16
Thread-3   _17
Thread-3   _18
Thread-3   _19
1

3种方式创建的优缺点

1.继承Thread显然有个很大的缺点,java是单继承了,如果继承了Thread那么就无法继承其他类。但是继承Thread编写简单,实现方便。

2.实现Runnable接口和Callable接口。大致一样,区别就是Callable接口的实现可以有返回值,且可以抛出显示异常。其余大致一样。
他们的优势是实现接口,那么实现类可以有其他父类,避免的Thread的问题,其次可以用一个实现了该接口的对象来创建多个线程,从而方便一些基本的资源共享,因为是同一个对象。