Spring教程____Spring线程池_ThreadPoolTaskExecutor的配置和使用

//1.搭建个简单的springmvc框架框架结构

Spring教程____Spring线程池_ThreadPoolTaskExecutor的配置和使用_第1张图片

2.applicationContext配置



	
	
	
		
			
				classpath:jdbc.properties
			
		
	  
	
	
	
	
	
	
	
	
		
		
		
		
	

	
	
		
	


	
	
		
		
		
		
		
		
		
		
		
		
			
		
	


3.spring-servlet的配置



	
	
		
		
		
	
3.web.xml配置



  springmvc
  
    index.jsp
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    contextConfigLocation
    classpath:spring-context.xml
  
  
    DispatcherServlet
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:spring-servlet.xml
    
  
  
    DispatcherServlet
    *.do
  

//测试sprng线程池的类

package com.spring.task.controller;

/**
 * @author King 根据姓名查询抢到车票的信息
 */
public class QueryTicket extends Thread {

	// 抢票人姓名
	private String username;

	//轮序的次数
	private int loopstep=1;
	//每次间隔的时间(秒)
	private int looptime=1;

	//构造器
	public QueryTicket(String username, int loopstep, int looptime) {
		super();
		this.username = username;
		this.loopstep = loopstep;
		this.looptime = looptime;
	}


	@Override
	public void run() {
		for (int i = 0; i < loopstep; i++) {
			//执行哪个类的哪个方法
			System.out.println("sql查询策略(执行3次每次间隔5秒)");
			System.out.println("_______姓名:"+username+"_第"+(i+1)+"次抢票状态:未抢到");
			try {
				Thread.sleep(looptime*1000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
}
package com.spring.task.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/task")
public class ThreadPoolController {

	@Autowired
    private	ThreadPoolTaskExecutor executor;
	
	@RequestMapping("/loopQueryTicket.do")
	public void loopQueryTicket(){
		QueryTicket queryTicket=new QueryTicket("zhagnsan", 3, 5);
		executor.execute(queryTicket);
	}
}

//运行结果

Spring教程____Spring线程池_ThreadPoolTaskExecutor的配置和使用_第2张图片

//源码地址:http://pan.baidu.com/s/1o8wr7S2


你可能感兴趣的:(Spring教程)