SpringBoot开启多线程

1、使用Runnable接口

import org.springframework.stereotype.Service;

@Service
public class MyService {

    public void executeAsyncTask() {
        Runnable task = () -> {
            // 在这里编写你的任务代码
            System.out.println("Running in a new thread: " + Thread.currentThread().getName());
            // 模拟耗时操作
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Task finished");
        };
        
        Thread thread = new Thread(task);
        thread.start(); // 启动新线程
    }
}
public class MyRunnableTask implements Runnable {

    @Override
    public void run() {
        // 在这里编写你的任务代码
        System.out.println("Running in a new thread: " + Thread.currentThread().getName());
        // 模拟耗时操作
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Task finished");
    }
}


import org.springframework.stereotype.Service;

@Service
public class MyService {

    public void executeAsyncTask() {
        MyRunnableTask task = new MyRunnableTask();
        Thread thread = new Thread(task);
        thread.start(); // 启动新线程
    }
}

2、使用ExecutorService

1. 配置ExecutorService Bean

在您的配置类中创建一个ExecutorService的Bean:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class AppConfig {

    @Bean
    public ExecutorService executorService() {
        // 创建一个固定大小的线程池
        return Executors.newFixedThreadPool(10);
    }
}
2. 创建一个任务类

创建一个实现了Runnable接口的任务类:

public class MyRunnableTask implements Runnable {

    @Override
    public void run() {
        // 任务代码
        System.out.println("Running in a new thread: " + Thread.currentThread().getName());
        // 模拟耗时操作
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt(); // 设置中断标志
            System.out.println("Task was interrupted");
        }
        System.out.println("Task finished");
    }
}
3. 在服务组件中提交任务

在服务组件中,注入ExecutorService并提交任务:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;

@Service
public class MyService {

    private final ExecutorService executorService;

    @Autowired
    public MyService(ExecutorService executorService) {
        this.executorService = executorService;
    }

    public void executeAsyncTask() {
        MyRunnableTask task = new MyRunnableTask();
        executorService.submit(task); // 提交任务给线程池执行
    }
}

3、使用Async

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class MyAsyncService {

    @Async
    public void asyncMethod() {
        // 执行异步操作
        System.out.println("Execute method asynchronously. " 
            + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        System.out.println("Async method execution completed.");
    }
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值