作者博客主页: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线程池由浅入深的3个示例的更多相关文章

  1. Spring线程池开发实战

    Spring线程池开发实战 作者:chszs,转载需注明. 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然, ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. spring线程池配置

    源自:http://zjriso.iteye.com/blog/771706 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurr ...

随机推荐

  1. Kubernetes Heapster

    Heapster是容器集群监控和性能分析工具,HPA.Dashborad.Kubectl top都依赖于heapster收集的数据. 但是Heapster从kubernetes 1.8以后已经被遗弃了 ...

  2. etcd 安装部署

    etcd 是coreos团队开发的分布式服务发现键值存储仓库. github地址: https://github.com/coreos/etcd 安装: 1.下载etcd最新版本 https://gi ...

  3. nginx最常见的18道面试题

    Nginx的并发能力在同类型网页服务器中的表现,相对而言是比较好的,因此受到了很多企业的青睐,我国使用Nginx网站的知名用户包括腾讯.淘宝.百度.京东.新浪.网易等等.Nginx是网页服务器运维人员 ...

  4. ML 感知机(Perceptrons)

    感知机 Perceptrons 学习Hinton神经网络公开课的学习笔记 https://class.coursera.org/neuralnets-2012-001 1 感知机历史 在19世纪60年 ...

  5. HTTP状态码 304

    HTTP 304 错误Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...

  6. Qt之密码框不可全选、复制、粘贴无右键菜单等

    转载---> http://blog.sina.com.cn/s/blog_a6fb6cc90101artk.html 在做用户登录界面的时候,往往会用到密码框,则其中的一些功能也要求与普通的输 ...

  7. Spring中AOP的初窥和入门小案例

    AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...

  8. Spring的DI初步

    DI:依赖注入 第一个DEMO:域属性注入 java类:(Car类和Stu类,学生有一辆小汽车) package cn.dawn.day02di; /** * Created by Dawn on 2 ...

  9. CentOS6.4x84挂载U盘

    root用户登录 1. 查看磁盘情况: fdisk -l 信息如下: [root@CentOS6 ~]# fdisk -l Disk /dev/sda: 128.8 GB, 128849018880 ...

  10. css3+jquery+js做的翻翻乐小游戏

    主要是为了练习一下css3的3D翻转功能,就做了这么个小游戏,做的比较粗糙,但是效果看的见. 主要用到的css3代码如下: html结构: <div class="container& ...