一、需求
在新调度平台中,我们使用了许多的队列,在任务量较大的情况下,我们必须保证从队列中取出任务并执行的效率。对于一些处理比较慢的任务,如果采用单线程,则会造成队列拥堵,我们需要使用多线程技术。
二、实现
1、config.properties中配置:
### THREAD POOL SETTINGS ###
cool_pool_size = 5
keep_alive_seconds = 200
max_pool_size = 10
2、applicationContext.xml中配置:
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" />
<bean id="threadPoolExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="${cool_pool_size}" />
<property name="keepAliveSeconds" value="${keep_alive_seconds}" />
<property name="maxPoolSize" value="${max_pool_size}" />
<property name="queueCapacity" value="20" />
<property name="rejectedExecutionHandler">
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
3、Config.java
@Component("Config")
public class Config
{
@Value("#{config.ftp_server_urls}")
private String ftp_server_urls;
@Value("#{config.max_queue_size}")
private int max_queue_size;
@Value("#{config.user_call_task_port}")
private int user_call_task_port;
@Value("#{config.distribute_thread_num}")
private int distribute_thread_num;
public String getFtp_server_urls()
{
return ftp_server_urls;
}
public int getMax_queue_size()
{
return max_queue_size;
}
public int getUser_call_task_port()
{
return user_call_task_port;
}
public int getDistribute_thread_num()
{
return distribute_thread_num;
}
}
4、DistributeTaskQueueProducer
@Component("DistributeTaskQueueProducer")
public class DistributeTaskQueueProducer
{
@Autowired
private TaskQueue taskQueue;
@Autowired
private ThreadPoolTaskExecutor threadPoolExecutor;
@Autowired
private Config config;
public void startThreadPool()
{
int distribute_thread_num = config.getDistribute_thread_num();
for(int i = 0; i < distribute_thread_num; i++)
{
DistributeTaskQueueThread distributeTaskQueueThread = new DistributeTaskQueueThread(taskQueue);
threadPoolExecutor.execute(distributeTaskQueueThread);
}
}
}
5、DistributeTaskQueueThread
public class DistributeTaskQueueThread implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(DistributeTaskQueueThread.class);
private BlockingQueue<Task> globalTaskqueue;
private volatile boolean stop = false;
/**
*
*/
public DistributeTaskQueueThread(TaskQueue taskQueue) {
this.globalTaskqueue =taskQueue.getPriorityTaskQueue();
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run()
{
System.out.println(this);
LOGGER.info("Distribute task queue producer start ...");
while (!Thread.currentThread().isInterrupted() && !stop) {
Task task = null;
try {
task = globalTaskqueue.take();
if (task != null) {
System.out.println(task.getTaskId());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}