- 浏览: 64290 次
- 性别:
- 来自: 西安
最新评论
-
obj704:
想问一下这个程序中的 run函数是承担了主动向客户端推送的任务 ...
java socketio server 端 笔记 -
vortexchoo:
很久没管博客了,今天才看到,我试了一下,好着呢。。确定实体类中 ...
利用反射处理map 与 实体类的值映射 -
ddsdggsg:
成功了,,,,不知大哥改进没有,,,我这传日期类型数据处理报错 ...
利用反射处理map 与 实体类的值映射 -
ddsdggsg:
请问怎么调用呢?刚试验,,还没成功
利用反射处理map 与 实体类的值映射 -
quainter:
for(a;b;c){
d;
}
1.第一次循环先执 ...
java--for
笔记:自己实现的线程池+队列。
package org.vic.thread.core;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public abstract class ThreadPool<T> implements Runnable {
private Integer defaultPoolSize = 10;
private Integer defaultQueueSize = 10;
private final ReentrantLock lock = new ReentrantLock();
private final Condition notEmpty = lock.newCondition();
private final Condition notFull = lock.newCondition();
private Long take_waittingMillis = 0L;
private Long put_warttingMillis = 0L;
private LinkedList<T> queue = new LinkedList<T>();
private boolean start = false;
public void take_waittingMills(Long val) {
this.take_waittingMillis = val;
}
public void put_waittingMills(Long val) {
this.put_warttingMillis = val;
}
/**
* @author Vic.Chu
* @param poolSize
* @param queueSize
* @param takeWaittingMillis
* @param putWaittingMillis
* Constructor
*/
public ThreadPool (Integer poolSize, Integer queueSize, Long takeWaittingMillis, Long putWaittingMillis) {
if (poolSize != null && poolSize > 0) {
defaultPoolSize = poolSize;
}
if (queueSize != null && queueSize > 0) {
defaultQueueSize = queueSize;
}
if (takeWaittingMillis != null && takeWaittingMillis > 0) {
take_waittingMills(takeWaittingMillis);
}
if (putWaittingMillis != null && putWaittingMillis > 0) {
put_waittingMills(putWaittingMillis);
}
}
/**
* @author Vic.Chu
* Constructor
*/
public ThreadPool () {}
/**
* threads will invoke this method after starting.
*/
@Override
public void run() {
try {
while (true) {
T t = take();
if (t == null) continue;
doProcess(t);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* @author Vic.Chu
* @param t
* Programmers have to implement this method for processing their data.
* @return
*/
public abstract T doProcess(T t);
/**
* @author Vic.Chu
* When new this ThreadPool, you should start it!
*/
public void start() {
if (!start) {
for (int i = 0; i < defaultPoolSize; i++) {
Thread thread = new Thread(this);
thread.start();
}
this.start = true;
}
}
/**
* @author Vic.Chu
* @param collection. the data collection which should be processed.
* @throws Exception. If the collection is null or is empty, this exception will be thrown.
*/
public void putCollections (Collection<? extends T> collection) throws Exception {
if (collection == null || collection.size() == 0) {
throw new Exception("Collection is null or is empty!");
}
for (T t : collection) {
try {
this.put(t);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @author Vic.Chu
* @param t
* @throws InterruptedException
* Put a data element into the queue, await the thread to process.
*/
private void put(T t) throws InterruptedException {
lock.lock();
try {
if (queue.size() >= defaultQueueSize) {
notFull.await();
} else {
notEmpty.signal();
Thread.sleep(this.put_warttingMillis);
queue.add(t);
}
} finally {
lock.unlock();
}
}
/**
* @author Vic.Chu
* @return
* @throws InterruptedException
* Take a data element from the queue and destroy this data element in the queue.
*/
private T take() throws InterruptedException {
lock.lock();
try {
if (queue.size() == 0) {
notEmpty.await();
return null;
} else {
notFull.signal();
Thread.sleep(this.take_waittingMillis);
T t = queue.getFirst();
queue.removeFirst();
return t;
}
} finally {
lock.unlock();
}
}
/**
* @author Vic.Chu
* @return
* Get data elements count in the queue.
*/
public Integer currentQueueSize () {
return queue.size();
}
/**
* @author Vic.Chu
* @return
* Get this thread pool's size.
*/
public Integer poolSize () {
return this.defaultPoolSize;
}
/**
* @author Vic.Chu
* @return
* Get this queue's size
*/
public Integer queueSize () {
return this.defaultQueueSize;
}
/**
* test
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ThreadPool<String> tp = new ThreadPool<String>(5, 5, 0L, 0L) {
@Override
public String doProcess(String t) {
System.out.println("current process is "
+ Thread.currentThread().getName());
System.out.println(t);
return t;
}
};
tp.start();
tp.putCollections(Arrays.asList(new String[]{"s","t","r","i","n","g"}));
}
}
package org.vic.thread.core;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public abstract class ThreadPool<T> implements Runnable {
private Integer defaultPoolSize = 10;
private Integer defaultQueueSize = 10;
private final ReentrantLock lock = new ReentrantLock();
private final Condition notEmpty = lock.newCondition();
private final Condition notFull = lock.newCondition();
private Long take_waittingMillis = 0L;
private Long put_warttingMillis = 0L;
private LinkedList<T> queue = new LinkedList<T>();
private boolean start = false;
public void take_waittingMills(Long val) {
this.take_waittingMillis = val;
}
public void put_waittingMills(Long val) {
this.put_warttingMillis = val;
}
/**
* @author Vic.Chu
* @param poolSize
* @param queueSize
* @param takeWaittingMillis
* @param putWaittingMillis
* Constructor
*/
public ThreadPool (Integer poolSize, Integer queueSize, Long takeWaittingMillis, Long putWaittingMillis) {
if (poolSize != null && poolSize > 0) {
defaultPoolSize = poolSize;
}
if (queueSize != null && queueSize > 0) {
defaultQueueSize = queueSize;
}
if (takeWaittingMillis != null && takeWaittingMillis > 0) {
take_waittingMills(takeWaittingMillis);
}
if (putWaittingMillis != null && putWaittingMillis > 0) {
put_waittingMills(putWaittingMillis);
}
}
/**
* @author Vic.Chu
* Constructor
*/
public ThreadPool () {}
/**
* threads will invoke this method after starting.
*/
@Override
public void run() {
try {
while (true) {
T t = take();
if (t == null) continue;
doProcess(t);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* @author Vic.Chu
* @param t
* Programmers have to implement this method for processing their data.
* @return
*/
public abstract T doProcess(T t);
/**
* @author Vic.Chu
* When new this ThreadPool, you should start it!
*/
public void start() {
if (!start) {
for (int i = 0; i < defaultPoolSize; i++) {
Thread thread = new Thread(this);
thread.start();
}
this.start = true;
}
}
/**
* @author Vic.Chu
* @param collection. the data collection which should be processed.
* @throws Exception. If the collection is null or is empty, this exception will be thrown.
*/
public void putCollections (Collection<? extends T> collection) throws Exception {
if (collection == null || collection.size() == 0) {
throw new Exception("Collection is null or is empty!");
}
for (T t : collection) {
try {
this.put(t);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @author Vic.Chu
* @param t
* @throws InterruptedException
* Put a data element into the queue, await the thread to process.
*/
private void put(T t) throws InterruptedException {
lock.lock();
try {
if (queue.size() >= defaultQueueSize) {
notFull.await();
} else {
notEmpty.signal();
Thread.sleep(this.put_warttingMillis);
queue.add(t);
}
} finally {
lock.unlock();
}
}
/**
* @author Vic.Chu
* @return
* @throws InterruptedException
* Take a data element from the queue and destroy this data element in the queue.
*/
private T take() throws InterruptedException {
lock.lock();
try {
if (queue.size() == 0) {
notEmpty.await();
return null;
} else {
notFull.signal();
Thread.sleep(this.take_waittingMillis);
T t = queue.getFirst();
queue.removeFirst();
return t;
}
} finally {
lock.unlock();
}
}
/**
* @author Vic.Chu
* @return
* Get data elements count in the queue.
*/
public Integer currentQueueSize () {
return queue.size();
}
/**
* @author Vic.Chu
* @return
* Get this thread pool's size.
*/
public Integer poolSize () {
return this.defaultPoolSize;
}
/**
* @author Vic.Chu
* @return
* Get this queue's size
*/
public Integer queueSize () {
return this.defaultQueueSize;
}
/**
* test
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ThreadPool<String> tp = new ThreadPool<String>(5, 5, 0L, 0L) {
@Override
public String doProcess(String t) {
System.out.println("current process is "
+ Thread.currentThread().getName());
System.out.println(t);
return t;
}
};
tp.start();
tp.putCollections(Arrays.asList(new String[]{"s","t","r","i","n","g"}));
}
}
发表评论
-
get req/resp in aspect
2018-11-27 09:24 323// HttpServletRequest req = ( ... -
Spring cloud
2018-04-17 16:26 410这个博客写的不错。 http://www.ityoukno ... -
设计模式
2017-02-28 15:45 294温习了一波java设计模式,没看完。把一些觉得比较经典的模式 ... -
compare local files and remote files.
2016-09-08 14:27 462最近发生了一件怪事,项目在本地run的时候会报一个jar包下 ... -
Servlet 3.0 使用request.part 处理文件上传
2016-08-26 14:14 721Servlet3.0 提供了比较简单的文件上传的api,今 ... -
不用其他jar包,获取随机排列数组。
2016-08-23 16:19 466/* * 网上看到一道题,挺有意思。记录下来。 */ ... -
简单的 threadlocal 模拟 及测试
2016-07-15 17:22 689package org.vic.demo.ThreadLo ... -
模拟简单的 spring component scan
2016-07-14 15:31 617/** * specified character ... -
eclipse 使用
2015-12-14 11:23 450同事整理的,挂在这分享、备用。 -
基于ReentrantLock 队列笔记
2015-12-11 12:40 662package org.vic.queue.core; ... -
java序列化
2015-11-24 17:02 368今天有同事问我动态读写对象的问题,于是就想到了序列化。在此做 ... -
Spring AOP 用法笔记
2015-11-17 17:42 784最近工作不太忙,把spring aop又重新看了一遍,今天 ... -
JVM总结
2015-11-05 11:14 1030有人问我: 你平时 ... -
java反射-实体类转xml jar包
2015-10-23 14:31 832同上一篇一样,避免重复造车轮,写了这个jar包。 引入ja ... -
基于LinkedBlockQueue 的消息队列jar包
2015-10-22 15:58 971为了让以后自己不再重复早轮子写的。 使用方法: ... -
线程的阻塞及唤醒
2015-10-15 18:01 599demo: 为方便展示,这里有两个类 : Taske ... -
自定义泛型的数组List
2015-10-09 10:12 685public class MyArrayList<T& ... -
HttpClient 模拟登录保持session并继续进行登录后操作
2015-09-08 13:05 8294公司考勤签到,每天都要登录系统签到,于是就在想:如果可以每 ... -
关闭流方法笔记
2015-09-01 09:19 627public class CloseHelper { ... -
Java回调(钩子函数)笔记(思想)
2015-08-27 11:23 1170Java回调(钩子函数): 拥有某些接口,但不关心实现,具 ...
相关推荐
ThreadPool 线程池+任务队列
保证线程安全的Qt线程池使用,线程任务可以和其他线程通信,使用场景:频繁操作的I/O操作或是耗时操作。
网上有现成的cthreadpool的代码,是c版的,且没有任务队列功能,不能做到任务的安全退出,根据实际需要写了一个c++版的,本程序实现了c++的封装,并实现了任务池和线程安全退出。弥补了c版的不足 在linux调试通过并...
NULL 博文链接:https://j2ee2009.iteye.com/blog/689161
线程池各类区别使用场景, 工作久了才知道理论的重要性。
Android中的线程池与任务队列
讲述线程池原理,线程池使用场景和注意事项,手动创建线程池方法,注意事项,阻塞队列的相关知识
线程池
并发-线程池和阻塞队列 并发-线程池和阻塞队列 并发-线程池和阻塞队列
windows 线程池demo,示例,简单明了,适用范围广,容易上手使用,实现了最基本的线程池
c#实现的工作队列,workquere,多线程管理-c# realization of the work queue, workquere, multi-threaded management
该源代码使用linux的网络IO多路复用模型epoll在建立tcpserver,对用队列来缓存请求,使用线程池来处理请求。
多线程大师Brian Goetz的文章,比国内的很多翻译过来的著作好的多,英语好的同学可以看看
线程池远不是服务器应用程序内使用多线程的唯一方法。如同上面所提到的,有时,为每个新任务生成一个新线程是十分明智的。然而,如果任务创建过于频繁而任务的平均处理时间过短,那么为每个任务生成一个新线程将会...
主要介绍了JVM优先级线程池做任务队列的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了java线程池工作队列饱和策略代码示例,涉及线程池的简单介绍,工作队列饱和策略的分析及代码示例,具有一定参考价值,需要的朋友可以了解下。
一、线程池 1、为什么需要使用线程池 1.1 创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处理效率。 记创建线程消耗时间T1,执行任务消耗时间T2,销毁线程消耗时间T3,如果T1+T3>T2,那...
几乎在每个服务器应用程序中都会出现线程池和工作队列问题。本文中,Brian Goetz 探讨了线程池的动机、一些基本实现和调优技术以及一些要避免的常见危险。
定义全局线程池,将用户的请求放入自定义队列中,排队等候线程调用,等待超时则自动取消该任务,实现超时可取消的异步任务
java.util.concurrent 使用jdk内置的包实现一个线程池的操作