Java线程的创建方式

Java线程的创建方式

1 继承Thread类

// 继承Thread类
public class ThreadTest extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread() + ":" + i);
        }
    }

    public static void main(String[] args) {
        ThreadTest t1 = new ThreadTest();
        ThreadTest t2 = new ThreadTest();
        ThreadTest t3 = new ThreadTest();

        t1.start();
        t2.start();
        t3.start();
    }
}

2 实现Runnable接口

//实现Runnable接口
class ThreadTest1 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread() + ":" + i);
        }
    }

    public static void main(String[] args) {
        ThreadTest1 t1 = new ThreadTest1();
        ThreadTest1 t2 = new ThreadTest1();
        ThreadTest1 t3 = new ThreadTest1();


        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t2);
        Thread thread3 = new Thread(t3);

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

3 实现Callable接口

3.1 Callable接口和Runnable接口的区别

1、Callable接口规定的方法是call方法,Runnable接口规定的方法是run方法;

2、Callable任务执行后有返回值,Runnable任务执行没有返回值;

3、Callable的call方法可以抛出异常,Runnable的run方法不会抛出异常;

4、通过Callable任务可以拿到一个Future对象,表示异步计算的结果,通过Future可以得到计算的结果,判断是否执行完成,还可以取消任务。

3.2 Callable的两种执行方式

3.2.1 使用FutureTask类执行

FutureTask类实现了RunnableFuture接口,RunnableFuture接口继承了Runnable接口和Future接口,所以FutureTask是一个提供异步计算结果的任务。FutureTask的继承关系如下。

在这里插入图片描述

使用FutureTask执行Callable任务代码如下:

class CallableTest implements Callable<String> {
    private String name;

    public CallableTest(String name) {
        this.name = name;
    }

    @Override
    public String call() throws Exception {
        return name;
    }

    public static void main(String[] args) {
        Callable callable = new CallableTest("name");
        FutureTask futureTask = new FutureTask(callable);
        Thread thread = new Thread(futureTask);

        thread.start();

        try {
            System.out.println(futureTask.get().toString());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
3.2.1 使用线程池
class CallableTest implements Callable<String> {
    private String name;

    public CallableTest(String name) {
        this.name = name;
    }

    @Override
    public String call() throws Exception {
        return name;
    }

    public static void main(String[] args) {
        //创建一个大小为5的线程池
        ExecutorService pool = Executors.newFixedThreadPool(5);
        List<Future> futures = new ArrayList<>();
		// 创建Callable对象,通过线程池提交任务,并用Future获取执行结果
        for (int i = 0; i < 5; i++) {
            Callable c = new CallableTest("name" + i);
            Future future = pool.submit(c);
            futures.add(future);
        }

        for (int i = 0; i < futures.size(); i++) {
            try {
                System.out.println(futures.get(i).get().toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}

4 基于线程池创建

class ExecutorTest implements Runnable {
    private String name;
    public ExecutorTest(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println(name);
    }

    public static void main(String[] args) {
        // 提交多个线程任务并执行
        ExecutorService pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            pool.submit(new ExecutorTest("name" + i));
        }

        pool.shutdown();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值