栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java多线程/spring boot多线程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java多线程/spring boot多线程

总结
  1. Main方法上开启异步:@EnableAsync
  2. (可选)配置线程池:配置ThreadPoolTaskExecutor,不配置则默认使用SimpleAsyncTaskExecutor
  3. 异步方法:@Async(“线程池名”)异步注解,放到方法上,表示调用该方法的线程与执行此方法的线程异步(调用线程与执行线程异步)
注意

ThreadPoolTaskExecutor中的线程都是守护线程,用户线程执行完毕,杀死所有守护线程
ThreadPoolTaskExecutor对象调用shutdown()方法时是否等待任务执行完毕再尝试shutdown()

// true-等待正在执行任务的线程执行完毕再尝试shutdown(),false-不等待,直接shutdown(),以抛出java.lang.InterruptedException异常的方式shutdown
// true-类似java线程池的shutdown(), false-类似java线程池的shutdownNow()
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);

@Async使用注意

  • @Async失效:static修饰异步方法(spring boot不能扫描到这个方法)
  • @Async失效:一个类中的方法相互调用不会触发异步
  • @Transactional失效:@Async方法上使用@Transactional
使用

pom.xml



    4.0.0

    com.xcrj
    springbootbasic
    1.0-SNAPSHOT

    
        8
        8
    

    
        
        
            org.springframework.boot
            spring-boot-starter
            2.7.1
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            2.7.1
            test
        
        
            junit
            junit
            RELEASE
            test
        
    

main @EnableAsync

package com.xcrj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

// 开启异步
@EnableAsync
@SpringBootApplication
public class ApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationMain.class, args);
        System.out.println("启动");
    }
}

configuration ThreadPoolTaskExecutor

package com.xcrj.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class AsyncMeConfiguration {

    // 配置线程池ThreadPoolTaskExecutor threadPoolTaskExecutor01
    @Bean("threadPoolTaskExecutor01")
    public ThreadPoolTaskExecutor getAsyncExecutor() {
        
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        threadPoolTaskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
        threadPoolTaskExecutor.setQueueCapacity(200);
        threadPoolTaskExecutor.setKeepAliveSeconds(10);
        threadPoolTaskExecutor.setThreadNamePrefix("asyncMe");
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // true-等待正在执行任务的线程执行完毕再尝试shutdown(),false-不等待,直接shutdown(),以抛出java.lang.InterruptedException异常的方式shutdown
        // true-类似java线程池的shutdown(), false-类似java线程池的shutdownNow()
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

}

service方法 @Async

package com.xcrj.service;

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

@Service
public class AsyncMeService {
    // 使用threadPoolTaskExecutor01线程池中的线程异步执行service1
    @Async("threadPoolTaskExecutor01")
    public void service1() throws InterruptedException {
        System.out.println("开始服务1");
        // 执行任务耗时
        Thread.sleep(5000);
        System.out.println("结束服务1");
    }

    // 使用threadPoolTaskExecutor01线程池中的线程异步执行service2
    @Async("threadPoolTaskExecutor01")
    public void service2() throws InterruptedException {
        System.out.println("开始服务2");
        // 执行任务耗时
        Thread.sleep(2000);
        System.out.println("结束服务2");
    }
}

test

package com.xcrj.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestAsyncMeService {
    @Resource
    private AsyncMeService asyncMeService;
    @Resource(name = "threadPoolTaskExecutor01")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;


    @Test
    public void testAsync() throws InterruptedException {
        asyncMeService.service1();
        asyncMeService.service2();

        threadPoolTaskExecutor.shutdown();
        // ThreadPoolTaskExecutor中的线程都是守护线程,用户线程执行完毕,杀死所有守护线程
        Thread.sleep(10000);
        System.out.println("结束test线程");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1024733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号