博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA编码(41)—— 线程池队列执行任务(ThreadPoolQueue)(1)
阅读量:4309 次
发布时间:2019-06-06

本文共 1557 字,大约阅读时间需要 5 分钟。

废话少说,上代码

package com.sinosoft;import java.util.concurrent.*;/** * Created by xushuyi on 2017/4/9. */public class ThreadPoolQueue {    /**     * 定义线程池中最大的线程数量     */    private static final Integer THREADPOOLSIZE = 100;    /**     * 创建线程队列     */    private static final BlockingQueue
QUEUE = new LinkedBlockingQueue
(); /** * 创建线程池 */ protected static ExecutorService executorService = Executors.newFixedThreadPool(THREADPOOLSIZE); /** * 线程池任务调度执行 */ 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("执行任务结果:" + task[0]); } } }); } /** * 线程池中添加执行任务 * * @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中

例如:

 

转载于:https://www.cnblogs.com/xushuyi/articles/6686327.html

你可能感兴趣的文章
理解sizeof()
查看>>
Vue学习笔记之vue-cli脚手架安装和webpack-simple模板项目生成
查看>>
SqlServer 扩展属性
查看>>
优先队列
查看>>
String的Intern方法
查看>>
KRPANO资源分析工具下载720YUN全景图
查看>>
一些程序和工具
查看>>
java8 运算语法集
查看>>
IDEA关于重命名
查看>>
Es6 中let var和 const 了解
查看>>
巧用队列之”Voting“
查看>>
Oracle数据类型number(m,n)
查看>>
ACC 001 C - Shorten Diameter 图论
查看>>
开通博客了...
查看>>
[转]复制虚拟机后linux中的eth0变成eth1问题
查看>>
TableViewCell中自定义XIB的使用
查看>>
ubuntu 显示隐藏文件
查看>>
Linux 定时任务crontab
查看>>
mongoose联表查询与一般查询合并
查看>>
jQuery--内容过滤和可见性过滤
查看>>