新任务管理系统YYSchedule-细节-spring整合线程池

新调度平台使用大量队列,任务量大时需保证取任务和执行的效率。对于处理慢的任务,单线程会导致队列拥堵,因此需采用多线程技术。还介绍了在config.properties、applicationContext.xml等文件及相关类中的实现配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、需求

在新调度平台中,我们使用了许多的队列,在任务量较大的情况下,我们必须保证从队列中取出任务并执行的效率。对于一些处理比较慢的任务,如果采用单线程,则会造成队列拥堵,我们需要使用多线程技术。

二、实现

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();
			}
		}
		
	}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值