spring的多线程配置

API:

Method Summary
 void afterPropertiesSet()
          Calls initialize() after the container applied all property values.
protected  edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue createQueue(int queueCapacity)
          Create the BlockingQueue to use for the ThreadPoolExecutor.
 void destroy()
          Calls shutdown when the BeanFactory destroys the task executor instance.
 void execute(Runnable task)
          Implementation of both the JSR-166 backport Executor interface and the Spring TaskExecutor interface, delegating to the ThreadPoolExecutor instance.
 void execute(Runnable task, long startTimeout)
          Execute the given task.
 int getActiveCount()
          Return the number of currently active threads.
 int getCorePoolSize()
          Return the ThreadPoolExecutor's core pool size.
 int getKeepAliveSeconds()
          Return the ThreadPoolExecutor's keep-alive seconds.
 int getMaxPoolSize()
          Return the ThreadPoolExecutor's maximum pool size.
 int getPoolSize()
          Return the current pool size.
 edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor getThreadPoolExecutor()
          Return the underlying ThreadPoolExecutor for native access.
 void initialize()
          Creates the BlockingQueue and the ThreadPoolExecutor.
 boolean prefersShortLivedTasks()
          This task executor prefers short-lived work units.
 void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut)
          Specify whether to allow core threads to time out.
 void setBeanName(String name)
          Set the name of the bean in the bean factory that created this bean.
 void setCorePoolSize(int corePoolSize)
          Set the ThreadPoolExecutor's core pool size.
 void setKeepAliveSeconds(int keepAliveSeconds)
          Set the ThreadPoolExecutor's keep-alive seconds.
 void setMaxPoolSize(int maxPoolSize)
          Set the ThreadPoolExecutor's maximum pool size.
 void setQueueCapacity(int queueCapacity)
          Set the capacity for the ThreadPoolExecutor's BlockingQueue.
 void setRejectedExecutionHandler(edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler rejectedExecutionHandler)
          Set the RejectedExecutionHandler to use for the ThreadPoolExecutor.
 void setThreadFactory(edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory threadFactory)
          Set the ThreadFactory to use for the ThreadPoolExecutor's thread pool.
 void setThreadNamePrefix(String threadNamePrefix)
          Specify the prefix to use for the names of newly created threads.
 void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
          Set whether to wait for scheduled tasks to complete on shutdown.
 void shutdown()
          Perform a shutdown on the ThreadPoolExecutor.
Future
submit(Callable task)
          Submit a Callable task for execution, receiving a Future representing that task.
 Future submit(Runnable task)
          Submit a Runnable task for execution, receiving a Future representing that task.

executor.xml的配置如下:




	


	

	
	
		
		
		
		
		
		
		
		
		
		
			
			
			
			
			
		
		
	


测试类ExecutorTest1.java的代码:
package com.soecode.lyf.web.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class ExecutorTest1 {


	@Test
	public void test() {

		try {

			ApplicationContext context1  = new ClassPathXmlApplicationContext("executor.xml"); 
			ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor)context1.getBean("taskExecutor");
			for(int i=0;i<10;i++){
				taskExecutor.execute(new Runnable(){
					@Override
					public void run() {
						System.out.println(Thread.currentThread());
					}
				});
			}
			System.out.println("main方法结束");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

打印的日志:

[org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@55816088: startup date [Thu Feb 08 15:16:54 CST 2018]; root of context hierarchy
  [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [executor.xml]
  [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] - Initializing ExecutorService  'taskExecutor'
  Thread[taskExecutor-1,5,main]
Thread[taskExecutor-3,5,main]
Thread[taskExecutor-4,5,main]
Thread[taskExecutor-2,5,main]
main方法结束
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-1,5,main]
Thread[taskExecutor-2,5,main]
Thread[taskExecutor-5,5,main]


你可能感兴趣的:(后端)