当前位置:编程学习 > JAVA >>

Spring线程池开发实战

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

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

 
下面开始。

注:项目源码已经托管到GitHub,
 

例子1:Spring结合Java线程。
通过继承Thread创建一个简单的Java线程,然后使用@Component让Spring容器管理此线程,Bean的范围必须是prototype,因此每个请求都会返回一个新实例,运行每个单独的线程。
PrintThread.java
[java] 
package com.chszs.thread; 
 
import org.springframework.stereotype.Component; 
import org.springframework.context.annotation.Scope; 
 
@Component 
@Scope("prototype") 
public class PrintThread extends Thread{ 
        @Override 
        public void run(){ 
                System.out.println(getName() + " is running."); 
                try{ 
                        Thread.sleep(5000); 
                }catch(InterruptedException e){ 
                        e.printStackTrace(); 
                } 
                System.out.println(getName() + " is running again."); 
        } 

AppConfig.java
[java] 
package com.chszs.config; 
 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
 
@Configuration 
@ComponentScan(basePackages="com.chszs.thread") 
public class AppConfig { 

App.java
[java] 
package com.chszs; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
 
import com.chszs.config.AppConfig; 
import com.chszs.thread.PrintThread; 
 
public class App { 
        public static void main(String[] args){ 
                ApplicationContext ctx =  
            new AnnotationConfigApplicationContext(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
[java] 
package com.chszs.thread; 
 
public class PrintTask implements Runnable{ 
        String name; 
        public PrintTask(String name){ 
                this.name = name; 
        } 
        @Override 
        public void run() { 
                System.out.println(name + " is running."); 
                try{ 
                        Thread.sleep(5000); 
                }catch(InterruptedException e){ 
                        e.printStackTrace(); 
&

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,