java实现多线程的常用方式

继承Thread类

继承Thread类,重写run方法

package com.example.course.config;

public class TestAsync extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("hello world Thread: " + i);
        }
    }

    public static void main(String[] args) {
        TestAsync testAsync = new TestAsync();
        testAsync.start();
    }
}

运行结果
![在这在这里插入代码片里插入图片描述](https://img-blog.csdnimg.cn/b67c18e5fe024709acedf7c18cce3b75.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5aWL5paX55qE56OK5ZOl4oaSX-KGkg==,size_20,color_FFFFFF,t_70,g_se,x_16

实现Runnable接口

实现runnable接口,并且重写run方法

package com.example.course.config;

public class TestAsync implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("hello world Runnable: " + i);
        }
    }

    public static void main(String[] args) {
        TestAsync testAsync = new TestAsync();
        Thread thread = new Thread(testAsync);
        thread.start();
    }
}

运行结果
在这里插入图片描述

实现Callable接口

实现Callable接口,重写call方法,这种实现方式与前两个不一样,这种有返回值

package com.example.course.config;

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

public class TestAsync implements Callable {
    static Integer a = 0;
    @Override
    public Integer call() throws Exception {
        for (int i = 0; i < 10; i++) {
            a += i;
        }
        return a;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestAsync testAsync = new TestAsync();
        //使用FutureTask的尖括号里面的类型就是线程返回的数据类型
        FutureTask<Integer> task = new FutureTask<Integer>(testAsync);
        Thread thread = new Thread(task);
        thread.start();
        //任务是否执行完成
        System.out.println(task.isDone());
        //get方法会等待线程体执行完成再将结果返回
        Integer result = task.get();
        System.out.println(result);
    }
}

注意: task.get()会阻塞,如果在调用这个方法的时候任务还没有执行完成,则会一直阻塞到方法执行完成之后再返回结果(可以再call方法内添加sleep方法验证,程序执行到get方法时会等待一段时间才打印出结果);
运行结果
在这里插入图片描述

使用线程池创建线程

1、如果任务不需要返回值,则调用ExecutorService的execute方法

package com.example.course.config;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestAsync {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            executorService.execute(() -> {
                System.out.println("hello world Executors: " + index);
            });
        }
        //关闭资源
        executorService.shutdown();
    }
}

运行结果
在这里插入图片描述

2、如果任务需要返回值,则调用ExecutorService的submit方法

package com.example.course.config;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestAsync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            final int index = i;
            Future<String> future = executorService.submit(() -> {
                System.out.println("hello world Executors: " + index);
                return "submit: " + index;
            });
            String result = future.get();
            System.out.println(result);
        }
        //关闭资源
        executorService.shutdown();
    }
}

运行结果
在这里插入图片描述
注意:Excutors可以创建多种线程池:
1、newSingleThreadExecutor:创建一个单线程的线程池;
2、newCachedThreadPool:创建一个可缓存的线程池;
3、newFixedThreadPool:创建一个固定长度的线程池;
4、newScheduledThreadPool:创建一个固定长度的线程池,并且支持定时任务;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值