JAVA编码(41)—— 线程池队列执行任务(ThreadPoolQueue)(1)
生活随笔
收集整理的這篇文章主要介紹了
JAVA编码(41)—— 线程池队列执行任务(ThreadPoolQueue)(1)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
廢話少說,上代碼
package com.sinosoft;import java.util.concurrent.*;/*** Created by xushuyi on 2017/4/9.*/ public class ThreadPoolQueue {/*** 定義線程池中最大的線程數(shù)量*/private static final Integer THREADPOOLSIZE = 100;/*** 創(chuàng)建線程隊列*/private static final BlockingQueue<Object[]> QUEUE = new LinkedBlockingQueue<Object[]>();/*** 創(chuàng)建線程池*/protected static ExecutorService executorService = Executors.newFixedThreadPool(THREADPOOLSIZE);/*** 線程池任務調(diào)度執(zhí)行*/static {executorService.execute(new Runnable() {public void run() {while (!Thread.currentThread().isInterrupted()) {Object[] task = null;try {task = QUEUE.take();} catch (Exception e) {e.printStackTrace();}System.out.println("執(zhí)行任務結(jié)果:" + task[0]);}}});}/*** 線程池中添加執(zhí)行任務** @param task t*/public void newTaskToThreadPool(Object[] task) {QUEUE.offer(task);}public static void main(String[] args) {ThreadPoolQueue queue = new ThreadPoolQueue();Object[] task = null;for (int i = 0; i < 10; i++) {task = new Object[]{"xushuyi_" + i};queue.newTaskToThreadPool(task);}} }真正項目中,就不會再使用static來初始化任務,而是定義一個方法,比如:public void runnbaleInit(),那么在配置文件中需要將本方法注入到Bean中
例如:
<bean id="policyMicroProductServiceSpringImpl" class="com.sinosoft.wechatscreen.service.impl.PolicyMicroProductServiceSpringImpl" init-method="runnbaleInit"></bean>?
轉(zhuǎn)載于:https://www.cnblogs.com/xushuyi/articles/6686327.html
總結(jié)
以上是生活随笔為你收集整理的JAVA编码(41)—— 线程池队列执行任务(ThreadPoolQueue)(1)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统课设实验五---Nachos文件
- 下一篇: Android 获取光线强弱,开启闪光灯