java 线程池 spring_Spring线程池开发实战及使用spring注解

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

前提条件:

1)在Eclipse创建一个Java项目,我取名为SpringThreadDemo。

2)项目所需的JAR包如图所示:

8b43ff840d18508a58cc46c683d34543.png

下面开始。

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

例子1:Spring结合Java线程。

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

PrintThread.java

packagecom.chszs.thread;

importorg.springframework.stereotype.Component;

importorg.springframework.context.annotation.Scope;

@Component

@Scope("prototype")

publicclassPrintThreadextendsThread{

@Override

publicvoidrun(){

System.out.println(getName() + " is running.");

try{

Thread.sleep(5000);

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.println(getName() + " is running again.");

}

}

AppConfig.java

packagecom.chszs.config;

importorg.springframework.context.annotation.ComponentScan;

importorg.springframework.context.annotation.Configuration;

@Configuration

@ComponentScan(basePackages="com.chszs.thread")

publicclassAppConfig {

}

App.java

packagecom.chszs;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.annotation.AnnotationConfigApplicationContext;

importcom.chszs.config.AppConfig;

importcom.chszs.thread.PrintThread;

publicclassApp {

publicstaticvoidmain(String[] args){

ApplicationContext ctx =

newAnnotationConfigApplicationContext(AppConfig.class);

PrintThread printThread1 = (PrintThread)ctx.getBean("printThread");

printThread1.setName("Thread 1");

PrintThread printThread2 = (PrintThread)ctx.getBean("printThread");

printThread2.setName("Thread 2");

PrintThread printThread3 = (PrintThread)ctx.getBean("printThread");

printThread3.setName("Thread 3");

PrintThread printThread4 = (PrintThread)ctx.getBean("printThread");

printThread4.setName("Thread 4");

PrintThread printThread5 = (PrintThread)ctx.getBean("printThread");

printThread5.setName("Thread 5");

printThread1.start();

printThread2.start();

printThread3.start();

printThread4.start();

printThread5.start();

}

}

输出:

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

packagecom.chszs.thread;

publicclassPrintTaskimplementsRunnable{

String name;

publicPrintTask(String name){

this.name = name;

}

@Override

publicvoidrun() {

System.out.println(name + " is running.");

try{

Thread.sleep(5000);

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.println(name + " is running again.");

}

}

Spring-Config.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.1.xsd">

class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

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

91cd93118ebc1ef5390fdaaa77b36b86.png

App1.java

packagecom.chszs;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

importcom.chszs.thread.PrintTask;

publicclassApp1 {

publicstaticvoidmain(String[] args) {

ApplicationContext ctx =

newClassPathXmlApplicationContext("resources/Spring-Config.xml");

ThreadPoolTaskExecutor taskExecutor =

(ThreadPoolTaskExecutor)ctx.getBean("taskExecutor");

taskExecutor.execute(newPrintTask("Thread 1"));

taskExecutor.execute(newPrintTask("Thread 2"));

taskExecutor.execute(newPrintTask("Thread 3"));

taskExecutor.execute(newPrintTask("Thread 4"));

taskExecutor.execute(newPrintTask("Thread 5"));

// 检查活动的线程,如果活动线程数为0则关闭线程池

for(;;){

intcount = taskExecutor.getActiveCount();

System.out.println("Active Threads : "+ count);

try{

Thread.sleep(1000);

}catch(InterruptedException e){

e.printStackTrace();

}

if(count==0){

taskExecutor.shutdown();

break;

}

}

}

}

输出:

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

packagecom.chszs.thread;

importorg.springframework.context.annotation.Scope;

importorg.springframework.stereotype.Component;

@Component

@Scope("prototype")

publicclassPrintTask2implementsRunnable {

String name;

publicvoidsetName(String name) {

this.name = name;

}

@Override

publicvoidrun(){

System.out.println(name + " is running.");

try{

Thread.sleep(5000);

}catch(InterruptedException e){

e.printStackTrace();

}

System.out.println(name + " is running again.");

}

}

AppConfig.java

packagecom.chszs.config;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.ComponentScan;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration

@ComponentScan(basePackages="com.chszs.thread")

publicclassAppConfig {

@Bean

publicThreadPoolTaskExecutor taskExecutor(){

ThreadPoolTaskExecutor pool = newThreadPoolTaskExecutor();

pool.setCorePoolSize(5);

pool.setMaxPoolSize(10);

pool.setWaitForTasksToCompleteOnShutdown(true);

returnpool;

}

}

App2.java

packagecom.chszs;

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.annotation.AnnotationConfigApplicationContext;

importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

importcom.chszs.config.AppConfig;

importcom.chszs.thread.PrintTask2;

publicclassApp2 {

publicstaticvoidmain(String[] args) {

ApplicationContext ctx =

newAnnotationConfigApplicationContext(AppConfig.class);

ThreadPoolTaskExecutor taskExecutor =

(ThreadPoolTaskExecutor)ctx.getBean("taskExecutor");

PrintTask2 printTask1 = (PrintTask2)ctx.getBean("printTask2");

printTask1.setName("Thread 1");

taskExecutor.execute(printTask1);

PrintTask2 printTask2 = (PrintTask2)ctx.getBean("printTask2");

printTask2.setName("Thread 2");

taskExecutor.execute(printTask2);

PrintTask2 printTask3 = (PrintTask2)ctx.getBean("printTask2");

printTask3.setName("Thread 3");

taskExecutor.execute(printTask3);

for(;;){

intcount = taskExecutor.getActiveCount();

System.out.println("Active Threads : "+ count);

try{

Thread.sleep(1000);

}catch(InterruptedException e){

e.printStackTrace();

}

if(count==0){

taskExecutor.shutdown();

break;

}

}

}

}

输出:

获取【下载地址】 【企业框架源码】 SpringMVC mybatis or hibernate ehcache二级缓存maven非和maven版本

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值