004.多线程-线程的三种创建方式

版权声明:本文为博主原创文章,允许转载,请标明出处。 https://blog.csdn.net/qwdafedv/article/details/84063075

1. extend Thread

package cn.qbz.thread;

public class ExtendThreadTest {
    public static void main(String[] args) {
        ExtendThread thread = new ExtendThread();
        thread.start();
        for (int i = 0; i < 300; i++) {
            System.out.println("主线程:" + Thread.currentThread().getName() + "  " + i);
        }
    }
}

class ExtendThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.println("子线程:" + this.getName() + "  " + i);
        }
    }
}


2. implement Runnable

package cn.qbz.thread;

public class ImplementRunnableTest {
    public static void main(String[] args) {
        ImplementRunnable runnable = new ImplementRunnable();
        Thread thread = new Thread(runnable);
        thread.start();

        for (int i = 0; i < 300; i++) {
            System.out.println("主线程:" + Thread.currentThread().getName() + "  " + i);
        }
    }
}

class ImplementRunnable implements Runnable {

    public void run() {
        for (int i = 0; i < 300; i++) {
            System.out.println("子线程:" + Thread.currentThread().getName() + "  " + i);
        }
    }
}


3.implement Callable

package cn.qbz.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeoutException;

public class ImplementCallableTest {
    public static void main(String[] args) {
        ImplementCallable callable = new ImplementCallable(666);
        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);
        Thread thread = new Thread(futureTask);
        thread.start();

        for (int i = 0; i < 300; i++) {
            System.out.println("主线程:" + Thread.currentThread().getName() + "  " + i);
        }

        try {
            System.out.println("子线程返回值:" + futureTask.get());
        } catch (InterruptedException e) {
            System.out.println("eeeeeeeeeeeeee222....." + e.getMessage());
        } catch (ExecutionException e) {
            System.out.println("eeeeeeeeeeeeee....." + e.getMessage());
        }
        for (int j = 0; j < 10; j++) {
            System.out.println("jjjjjjjjjjj  " + j);
        }
    }
}

class ImplementCallable implements Callable<Integer> {
    private Integer result;

    public ImplementCallable(Integer result) {
        this.result = result;
    }

    public Integer call() throws Exception {
        for (int i = 0; i < 300; i++) {
            System.out.println("子线程:" + Thread.currentThread().getName() + "  " + i);
            if (i == 268) {
                throw new TimeoutException();
            }
        }
        return result;
    }
}

  1. Implement Callable,重写call方法,不但可以返回值,还可以抛出异常。
  2. 抛出的异常,在futureTask.get()中被捕获
  3. futureTask.get()在执行的过程中会阻塞,一直到所在线程执行结束
  4. futureTask还有相关方法来进行了解线程的执行情况

三种创建方式的比较:

当前主流的就是面向接口开发,
因为实现了接口后还可以根据业务需要再继承其他的类。
实现Runnable接库与实现Callable接口对比来看,
Callable接口有几个优势,也就是上面所说的:
可返回值,可抛出异常,可了解线程执行情况。

猜你喜欢

转载自blog.csdn.net/qwdafedv/article/details/84063075