JUC-5.创建执行线程的方式

1. 创建执行线程的方式

创建执行线程一共有4种方式。分别是继承Thread、实现接口Runnable接口、实现Callable接口、线程池。

1.1 继承Thread

继承Thread并重写父类run()方法,通过start()方法启动线程。

/** * 创建执行线程方式一:继承Tread * @author xiaobin * @date 2018/3/11 */
public class TestThread {
    public static void main(String[] args) {
        ThreadDemo threadDemo1 = new ThreadDemo("A");
        ThreadDemo threadDemo2 = new ThreadDemo("B");
        threadDemo1.start();
        threadDemo2.start();
    }
}
class ThreadDemo extends Thread {
    public ThreadDemo(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("运行线程:" + this.getName());
    }
}

1.2 实现Runnable接口

通过实现Runnable接口,并实现方法run()完成任务。将Runnable实现类的实例传到Thread构造方法,完成任务执行。

/** * 创建执行线程方式二:实现Runnable * @author xiaobin * @date 2018/3/11 */
public class TestRunnable {
    public static void main(String[] args) {
        Thread th1 = new Thread(new RunnableDemo(),"A");
        Thread th2 = new Thread(new RunnableDemo(),"B");
        th1.start();
        th2.start();
    }
}

class RunnableDemo implements Runnable {

    @Override
    public void run() {
        System.out.println("运行线程:" + Thread.currentThread().getName());
    }
}

1.3 实现Callable接口

相对于Runnable接口的方式,方法可以有返回值,并且可以抛出异常。
Callable需要FutureTask实现类的支持,用于接收返回结果。get()方法是阻塞方法,直到有返回值。

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

/** * 创建执行线程方式三:实现Callable * @author xiaobin * @date 2018/3/11 */
public class TestCallable {
    public static void main(String[] args) {
        CallableDemo callableDemo = new CallableDemo();
        //Callable需要FutureTask实现类的支持,用于接收运算结果
        FutureTask<Integer> result = new FutureTask<>(callableDemo);
        new Thread(result).start();

        try {
            System.out.println("接收结果:" + result.get());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class CallableDemo implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("执行Callable");
        return 123;
    }
}

1.4 线程池

JUC包提供的Excetor为线程池接口,通过excute(Runnable r)方法可以将任务交给线程池进行执行。
Java提供了Executors类,该类有四个静态方法分别可以创建不同类型的线程池(ExecutorService)。
– Executors.newCachedThreadPool() 创建可变大小的线程池
– Executors.newFixedThreadPool(int number) 创建固定大小的线程池
– Executors.newSingleThreadPool() 创建单任务线程池
– Executors.newScheduledThreadPool(int number) 创建延迟线程池

/** * @author xiaobin * @date 2018/3/11 */
public class TestThreadPool {
    public static void main(String[] args) {

        //创建一个可重用固定线程数的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Thread t1 = new ThreadDemo1();
        Runnable runnable = new RunnableDemo1();
        Callable callable = new CallableDemo1();


        try {
            executorService.execute(runnable);
            executorService.execute(t1);
            Future<String> future1 = executorService.submit(callable);
            System.out.println("返回值:" + future1.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

}

class CallableDemo1 implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println("运行线程callable:" + Thread.currentThread().getName());
        return "123";
    }
}

class RunnableDemo1 implements Runnable {

    @Override
    public void run() {
        System.out.println("运行线程:" + Thread.currentThread().getName());
    }
}

class ThreadDemo1 extends Thread {

    @Override
    public void run() {
        System.out.println("运行线程:" + this.getName());
    }
}
    原文作者:JUC
    原文地址: https://blog.csdn.net/xbliu564/article/details/79517163
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞