Spring线程池开发实战

作者:chszs,转载需注明。

作者博客主页:http://blog.csdn.net/chszs

本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释。诸位一看便知。

前提条件:

1)在Eclipse创建一个Java项目,我取名为SpringThreadDemo。
2)项目所需的JAR包如图所示:
 

下面开始。

注:项目源码已经托管到GitHub,地址:https://github.com/chszs/SpringThreadDemo

例子1:Spring结合Java线程。

通过继承Thread创建一个简单的Java线程,然后使用@Component让Spring容器管理此线程,Bean的范围必须是prototype,因此每个请求都会返回一个新实例,运行每个单独的线程。

PrintThread.java

  1. package com.chszs.thread;
  2. import org.springframework.stereotype.Component;
  3. import org.springframework.context.annotation.Scope;
  4. @Component
  5. @Scope("prototype")
  6. public class PrintThread extends Thread{
  7. @Override
  8. public void run(){
  9. System.out.println(getName() + " is running.");
  10. try{
  11. Thread.sleep(5000);
  12. }catch(InterruptedException e){
  13. e.printStackTrace();
  14. }
  15. System.out.println(getName() + " is running again.");
  16. }
  17. }

AppConfig.java

  1. package com.chszs.config;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @ComponentScan(basePackages="com.chszs.thread")
  6. public class AppConfig {
  7. }

App.java

  1. package com.chszs;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import com.chszs.config.AppConfig;
  5. import com.chszs.thread.PrintThread;
  6. public class App {
  7. public static void main(String[] args){
  8. ApplicationContext ctx =
  9. new AnnotationConfigApplicationContext(AppConfig.class);
  10. PrintThread printThread1 = (PrintThread)ctx.getBean("printThread");
  11. printThread1.setName("Thread 1");
  12. PrintThread printThread2 = (PrintThread)ctx.getBean("printThread");
  13. printThread2.setName("Thread 2");
  14. PrintThread printThread3 = (PrintThread)ctx.getBean("printThread");
  15. printThread3.setName("Thread 3");
  16. PrintThread printThread4 = (PrintThread)ctx.getBean("printThread");
  17. printThread4.setName("Thread 4");
  18. PrintThread printThread5 = (PrintThread)ctx.getBean("printThread");
  19. printThread5.setName("Thread 5");
  20. printThread1.start();
  21. printThread2.start();
  22. printThread3.start();
  23. printThread4.start();
  24. printThread5.start();
  25. }
  26. }

输出:

Thread 1 is running.
Thread 2 is running.
Thread 4 is running.
Thread 5 is running.
Thread 3 is running.
Thread 2 is running again.
Thread 1 is running again.
Thread 5 is running again.
Thread 4 is running again.
Thread 3 is running again.

例子2:Spring线程池结合非Spring托管Bean。

使用Spring的ThreadPoolTaskExecutor类创建一个线程池。执行线程无需受Spring容器的管理。

PrintTask.java

  1. package com.chszs.thread;
  2. public class PrintTask implements Runnable{
  3. String name;
  4. public PrintTask(String name){
  5. this.name = name;
  6. }
  7. @Override
  8. public void run() {
  9. System.out.println(name + " is running.");
  10. try{
  11. Thread.sleep(5000);
  12. }catch(InterruptedException e){
  13. e.printStackTrace();
  14. }
  15. System.out.println(name + " is running again.");
  16. }
  17. }

Spring-Config.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.1.xsd">
  8. <bean id="taskExecutor"
  9. class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
  10. <property name="corePoolSize" value="5" />
  11. <property name="maxPoolSize" value="10" />
  12. <property name="WaitForTasksToCompleteOnShutdown" value="true" />
  13. </bean>
  14. </beans>

注意这个Spring配置文件的位置,如图所示:

App1.java

  1. package com.chszs;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  5. import com.chszs.thread.PrintTask;
  6. public class App1 {
  7. public static void main(String[] args) {
  8. ApplicationContext ctx =
  9. new ClassPathXmlApplicationContext("resources/Spring-Config.xml");
  10. ThreadPoolTaskExecutor taskExecutor =
  11. (ThreadPoolTaskExecutor)ctx.getBean("taskExecutor");
  12. taskExecutor.execute(new PrintTask("Thread 1"));
  13. taskExecutor.execute(new PrintTask("Thread 2"));
  14. taskExecutor.execute(new PrintTask("Thread 3"));
  15. taskExecutor.execute(new PrintTask("Thread 4"));
  16. taskExecutor.execute(new PrintTask("Thread 5"));
  17. // 检查活动的线程,如果活动线程数为0则关闭线程池
  18. for(;;){
  19. int count = taskExecutor.getActiveCount();
  20. System.out.println("Active Threads : " + count);
  21. try{
  22. Thread.sleep(1000);
  23. }catch(InterruptedException e){
  24. e.printStackTrace();
  25. }
  26. if(count==0){
  27. taskExecutor.shutdown();
  28. break;
  29. }
  30. }
  31. }
  32. }

输出:

Thread 1 is running.
Thread 2 is running.
Thread 3 is running.
Thread 4 is running.
Active Threads : 4
Thread 5 is running.
Active Threads : 5
Active Threads : 5
Active Threads : 5
Active Threads : 5
Active Threads : 5
Thread 4 is running again.
Thread 2 is running again.
Thread 3 is running again.
Thread 1 is running again.
Thread 5 is running again.
Active Threads : 0

作者:chszs,转载需注明。博客主页:http://blog.csdn.net/chszs

例子3:Spring线程池结合Spring托管Bean。

本例仍然使用ThreadPoolTaskExecutor类,并使用@Component注释声明Spring的托管Bean。
下面的例子PrintTask2是Spring的托管Bean,使用@Autowired注释简化代码。

PrintTask2.java

  1. package com.chszs.thread;
  2. import org.springframework.context.annotation.Scope;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. @Scope("prototype")
  6. public class PrintTask2 implements Runnable {
  7. String name;
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. @Override
  12. public void run(){
  13. System.out.println(name + " is running.");
  14. try{
  15. Thread.sleep(5000);
  16. }catch(InterruptedException e){
  17. e.printStackTrace();
  18. }
  19. System.out.println(name + " is running again.");
  20. }
  21. }

AppConfig.java

  1. package com.chszs.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  6. @Configuration
  7. @ComponentScan(basePackages="com.chszs.thread")
  8. public class AppConfig {
  9. @Bean
  10. public ThreadPoolTaskExecutor taskExecutor(){
  11. ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
  12. pool.setCorePoolSize(5);
  13. pool.setMaxPoolSize(10);
  14. pool.setWaitForTasksToCompleteOnShutdown(true);
  15. return pool;
  16. }
  17. }

App2.java

  1. package com.chszs;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  5. import com.chszs.config.AppConfig;
  6. import com.chszs.thread.PrintTask2;
  7. public class App2 {
  8. public static void main(String[] args) {
  9. ApplicationContext ctx =
  10. new AnnotationConfigApplicationContext(AppConfig.class);
  11. ThreadPoolTaskExecutor taskExecutor =
  12. (ThreadPoolTaskExecutor)ctx.getBean("taskExecutor");
  13. PrintTask2 printTask1 = (PrintTask2)ctx.getBean("printTask2");
  14. printTask1.setName("Thread 1");
  15. taskExecutor.execute(printTask1);
  16. PrintTask2 printTask2 = (PrintTask2)ctx.getBean("printTask2");
  17. printTask2.setName("Thread 2");
  18. taskExecutor.execute(printTask2);
  19. PrintTask2 printTask3 = (PrintTask2)ctx.getBean("printTask2");
  20. printTask3.setName("Thread 3");
  21. taskExecutor.execute(printTask3);
  22. for(;;){
  23. int count = taskExecutor.getActiveCount();
  24. System.out.println("Active Threads : " + count);
  25. try{
  26. Thread.sleep(1000);
  27. }catch(InterruptedException e){
  28. e.printStackTrace();
  29. }
  30. if(count==0){
  31. taskExecutor.shutdown();
  32. break;
  33. }
  34. }
  35. }
  36. }

输出:

Thread 1 is running.
Thread 2 is running.
Active Threads : 2
Thread 3 is running.
Active Threads : 3
Active Threads : 3
Active Threads : 3
Active Threads : 3
Thread 1 is running again.
Thread 2 is running again.
Thread 3 is running again.
Active Threads : 1
Active Threads : 0

从这三个简单的实例中,你是不是发现了Spring框架在多线程方面的强大之处!!

Spring线程池开发实战的更多相关文章

  1. Spring线程池由浅入深的3个示例

    作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释.诸位一看便知. 前提条件: 1)在Ec ...

  2. Spring线程池配置模板设计(基于Springboot)

    目录 线程池配置模板 基础的注解解释 常用配置参数 配置类设计 线程池使用 ThreadPoolTaskExecutor源码 线程池配置模板 springboot给我们提供了一个线程池的实现,它的底层 ...

  3. Spring线程池ThreadPoolTaskExecutor配置及详情

    Spring线程池ThreadPoolTaskExecutor配置及详情 1. ThreadPoolTaskExecutor配置 <!-- spring thread pool executor ...

  4. 分享知识-快乐自己:Spring线程池配置

    Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. Spring 配 ...

  5. 【SSM Spring 线程池 OJ】 使用Spring线程池ThreadPoolTaskExecutor

    最近做的Online Judge项目,在本地判题的实现过程中,遇到了一些问题,包括多线程,http通信等等.现在完整记录如下: OJ有一个业务是: 用户在前端敲好代码,按下提交按钮发送一个判题请求给后 ...

  6. spring线程池的同步和异步(1)

    spring线程池(同步.异步) 一.spring异步线程池类图 二.简单介绍 2.1. TaskExecutor---Spring异步线程池的接口类,其实质是java.util.concurrent ...

  7. 007-多线程-JUC线程池-Spring线程池配置、池子如何配置参数

    一.概述 Spring通过ThreadPoolTaskExecutor实现线程池技术,它是使用jdk中的Java.util.concurrent.ThreadPoolExecutor进行实现. 1.1 ...

  8. JDK线程池和Spring线程池的使用

    JDK线程池和Spring线程池实例,异步调用,可以直接使用 (1)JDK线程池的使用,此处采用单例的方式提供,见示例: public class ThreadPoolUtil { private s ...

  9. java和spring 线程池总结

    1. spring 的线程池 ThreadPoolTaskExecutor @Configuration public class ThreadPoolConfig { @Bean("thr ...

随机推荐

  1. 关于 div随网页居中问题

    可以先在外部设置个 宽高 小于浏览器的 div 内容再根据 最外层 定位 这个代码是 左右居中的 <div style=" width:300px; height:300px; mar ...

  2. matlab,xls转换为mat文件

    b=xlsread('iris_data.xls');save iris_data.mat b

  3. EntityFramework批量Insert

    先说解决办法:使用SqlBulkCopy. 然后问题是:这个和EF没有半点关系,还要拼DataSet. 再是解决办法:你可以自己封装一个,也可以使用人家写好的 EntityFramework.Bulk ...

  4. 2015 QQ最新登录算法

    首先还是取得验证码,抓包可得:http://check.ptlogin2.qq.com/check?regmaster=&pt_tea=1&uin=2630366651&app ...

  5. 2015 Syrian Private Universities Collegiate Programming Contest 题解

    题目在这里>_< 发现这场比赛在网上没有完整的题解,甚至连题目代码都没人贴出来(大概是因为题目太水了吧...).所以宝宝就来写个题解,也就当作成长记录了233333 A. Window 题 ...

  6. Remote小Demo

    Demo基于http://www.cnblogs.com/zhili/p/NETRemoting.html RemotingObj using System; using System.Collect ...

  7. Thrift框架简介

    功能:实现各个服务模块之间的跨语言.跨平台的通信,是RPC框架的一种,与dubbo类似. Thrift的应用原理: Thrift的部分功能相当于代码生成引擎,使用Thrift定义的语言编写*.Thri ...

  8. Django开发环境配置

    Eclipse   首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官网下载JDK安装:ht ...

  9. linux的命令使用记录

    iptables禁止53端口的出包(dns) iptables -A OUTPUT -p udp --dport 53 -j DROP linux查看网络监听端口 netstat -npl 文件复制 ...

  10. VHDL设计问题

    在做算术运算的时候,不可以用std_ulogic_vector,必须是std_logic_vector.