在spring中引入线程池,设置线程优先级。

在spring.xml 文件中配置:


    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
               
        <property name="corePoolSize" value="2" />  
            
        <property name="maxPoolSize" value="5" />  
          
        <property name="queueCapacity" value="10" />  
          
        <property name="keepAliveSeconds" value="200" />  
    bean>  

然后在要使用的服务中注入:

     /**
   * 引入线程
   */
  @Resource
  private TaskExecutor taskExecutor;

在方法中直接使用即可:

1,常规用法

    //获取线程执行时间计算
    taskExecutor.execute(new Runnable() {
        @Override
        public void run() {

            .........
        }
    });

2,调整优先级

Thread thread= new Thread(new Runnable() {
@Override
public void run() {
...................
}
});
thread.setPriority(10);
taskExecutor.execute(thread);

“`

你可能感兴趣的:(java)