Spring线程池开发实战

转载 2016年05月30日 23:19:54

作者: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

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

AppConfig.java

[java] view plain copy
 print?
  1. package com.chszs.config;  
  2.   
  3. import org.springframework.context.annotation.ComponentScan;  
  4. import org.springframework.context.annotation.Configuration;  
  5.   
  6. @Configuration  
  7. @ComponentScan(basePackages="com.chszs.thread")  
  8. public class AppConfig {  
  9. }  

App.java

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

输出:

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

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

Spring-Config.xml

[html] view plain copy
 print?
  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.           
  9.         <bean id="taskExecutor"   
  10.         class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
  11.                 <property name="corePoolSize" value="5" />  
  12.                 <property name="maxPoolSize" value="10" />  
  13.                 <property name="WaitForTasksToCompleteOnShutdown" value="true" />  
  14.         </bean>  
  15. </beans>  

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



App1.java

[java] view plain copy
 print?
  1. package com.chszs;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;  
  6.   
  7. import com.chszs.thread.PrintTask;  
  8.   
  9. public class App1 {  
  10.   
  11.         public static void main(String[] args) {  
  12.                 ApplicationContext ctx =   
  13.             new ClassPathXmlApplicationContext("resources/Spring-Config.xml");  
  14.                 ThreadPoolTaskExecutor taskExecutor =  
  15.             (ThreadPoolTaskExecutor)ctx.getBean("taskExecutor");  
  16.                 taskExecutor.execute(new PrintTask("Thread 1"));  
  17.                 taskExecutor.execute(new PrintTask("Thread 2"));  
  18.                 taskExecutor.execute(new PrintTask("Thread 3"));  
  19.                 taskExecutor.execute(new PrintTask("Thread 4"));  
  20.                 taskExecutor.execute(new PrintTask("Thread 5"));  
  21.                 // 检查活动的线程,如果活动线程数为0则关闭线程池  
  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.   
  37. }  

输出:

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

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

AppConfig.java

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

App2.java

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

输出:

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线程池开发实战

作者:chszs,转载需注明。 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释...

Spring线程池开发实战

http://www.2cto.com/kf/201211/170841.html 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释。...

Spring线程池开发实战

作者:chszs,转载需注明。 作者博客主页:http://blog.csdn.net/chszs 本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释。诸...

Spring线程池开发实战

作者:chszs,转载需注明。作者博客主页:http://blog.csdn.net/chszs本文提供了三个Spring多线程开发的例子,由浅入深,由于例子一目了然,所以并未做过多的解释。诸位一看便...

ActiveMQ与Spring线程池整合实例

  • 2008年02月21日 14:26
  • 47KB
  • 下载

如何使用Spring开发和监控线程池服务

线程池对执行同步或异步的任务很重要。本文展示如何利用Spring开发并监控线程池服务。创建线程池的其他两种方法已讲解过。 使用技术 JDK 1.6.0_21Spring 3.0.5Maven ...
  • wanglha
  • wanglha
  • 2016年01月18日 09:30
  • 721

Spring线程池demo

  • 2016年05月25日 15:01
  • 6KB
  • 下载

Spring线程池配置

  • 2015年02月11日 13:53
  • 35KB
  • 下载

Android自定义线程池的编程实战

1、Executor 简介 在Java 5之后,并发编程引入了一堆新的启动、调度和管理线程的API。Executor框架便是Java 5中引入的,其内部使用了线程池机制,它在java.util.co...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring线程池开发实战
举报原因:
原因补充:

(最多只允许输入30个字)