创建线程的三种方式

并发编程

创建线程的三种方式

1. 继承 Thread类

1、继承Thread类。并重写run()方法,该方法无参数,无返回值;

2、创建子类实例,并实例化对象;

3、通过start()方法启动,注意:不是通过run()方法启动。

public class ThreadDemo extends Thread{
    public void run(){
        System.out.println("hello ,我是通过继承Thread创建线程的。");
    }
}
public class ThreadAction {
    public static void main(String[] args) {
        ThreadDemo demo = new ThreadDemo();
        demo.start();
    }
}
2. 实现Runnable接口

1、实现Runnable接口,并重新run()方法;

2、创建Runnable实现类的实例,并用这个实例作为Thread的target创建线程对象;

3、通过start()启动线程。

public class RunnableDemo implements Runnable{
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        System.out.println("hello ,我是通过实现Runnable创建线程的。");
    }
}
    public static void main(String[] args) {
        RunnableDemo demo = new RunnableDemo();
        Thread thread = new Thread(demo);
        thread.start();
    }
3. 使用Callable接口

产生背景:这种创建线程的方式在jdk1.5后出现,目的是弥补上面两种创建线程方式,其中的run()方法不能有返回值,不能抛出异常等问题。

1、创建实现Callable的实现类,并实现call()方法;

2、创建该实现类的实例(从java8开始可以直接使用Lambda表达式创建Callable对象);

3、使用FutureTask类来包装Callable对象,该FutureTask对象封装了Callable对象的call()方法的返回值;

4、使用FutureTask对象作为Thread对象的target创建并启动线程(因为FutureTask实现了Runnable接口);

5、调用FutureTask对象的get()方法来获得子线程执行结束后的返回值。

public class CallableDemo implements Callable<String> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public String call() throws Exception {
        return "hello ,我是通过实现Callable接口的方式创建线程。";
    }
}
    public static void main(String[] args) {
        CallableDemo demo = new CallableDemo();
        // 使用FutureTask类来包装Callable对象
        FutureTask<String> futureTask = new FutureTask<>(demo);
        Thread thread = new Thread(futureTask);
        thread.start();
        try {
            System.out.println(futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
4. 线程池

后续深入探讨线程池

转载于:https://www.cnblogs.com/yanfei1819/p/8950371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值