- 浏览: 84823 次
文章分类
最新评论
-
bailangfei3344:
自我介绍 -
regionwar:
你好,转化为人为:1、不该加锁的不要加锁:局部变量,单线程占用 ...
关于java锁机制的优化 -
danni505:
希望能交流:
msn:danni-505#hotmail.co ...
关于java锁机制的优化 -
ouspec:
收藏的东西不错。
TOP500 -
willpower:
The idea behind is e-sync IO do ...
Rethink the sync
java 代码
- import java.util.Vector;
- /**
- * Thread pool
- */
- public class ThreadPool implements Runnable {
- // Default ThreadPool minimum size
- public final static int DEFAULT_MIN_SIZE = 0;
- // Default ThreadPool maximum size
- public final static int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
- public final static long DEFAULT_RELEASE_DELAY = 10 * 1000;
- // customized thread pool minimum size
- protected int minSize;
- // customized thread pool maximum size
- protected int maxSize;
- protected long releaseDelay;
- // current threads size in threadpool
- protected int currentSize;
- protected int availableThreads;
- // task list
- protected Vector taskList;
- /**
- * customized ThradPool
- *
- * @param minSize
- * minimum thread pool size
- * @param maxSize
- * maximum thread pool size
- * @param releaseDelay
- * threads release delay
- */
- public ThreadPool(int minSize, int maxSize, long releaseDelay) {
- this.minSize = minSize;
- this.maxSize = maxSize;
- this.releaseDelay = releaseDelay;
- taskList = new Vector(100);
- availableThreads = 0;
- }
- /**
- * Default ThreadPool
- */
- public ThreadPool() {
- this(DEFAULT_MIN_SIZE, DEFAULT_MIN_SIZE, DEFAULT_RELEASE_DELAY);
- }
- /**
- * set minimum thread pool size
- *
- * @param minSize
- * minimum thread pool size
- */
- public synchronized void setMinSize(int minSize) {
- this.minSize = minSize;
- }
- /**
- * get minimum thread pool size
- */
- public synchronized int getMinSize() {
- return minSize;
- }
- /**
- * set maximum thread pool size
- *
- * @param maxSize
- * maximum thread pool size
- */
- public synchronized void setMaxSize(int maxSize) {
- this.maxSize = maxSize;
- }
- /**
- * get maximum thread pool size
- */
- public synchronized int getMaxSize() {
- return maxSize;
- }
- /**
- * set thread release delay
- *
- * @param releaseDelay
- * thread release delay time
- */
- public synchronized void setReleaseDelay(long releaseDelay) {
- this.releaseDelay = releaseDelay;
- }
- /**
- * get thread release delay
- */
- public synchronized long getReleaseDelay() {
- return releaseDelay;
- }
- /**
- * add a task to task list of ThreadPool
- *
- * @param runnable
- * new task
- */
- public synchronized void addTask(Runnable runnable) {
- taskList.addElement(runnable);
- if (availableThreads > 0) {
- this.notify();
- } else {
- if (currentSize < maxSize) {
- Thread t = new Thread(this);
- currentSize++;
- t.start();
- }
- }
- }
- public void run() {
- Runnable task;
- while (true) {
- synchronized (this) {
- if (currentSize > maxSize) {
- currentSize--;
- break;
- }
- task = getNextTask();
- if (task == null) {
- try {
- availableThreads++;
- wait(releaseDelay);
- availableThreads--;
- } catch (InterruptedException ie) {
- // do something you wanna
- }
- task = getNextTask();
- if (task == null) {
- if (currentSize < minSize)
- continue;
- currentSize--;
- break;
- }
- }
- }
- try {
- task.run();
- } catch (Exception e) {
- System.err.println("Uncaught exception");
- e.printStackTrace(System.err);
- }
- }
- }
- /**
- * get the next task from task list.
- *
- */
- protected synchronized Runnable getNextTask() {
- Runnable task = null;
- if (taskList.size() > 0) {
- task = (Runnable) (taskList.elementAt(0));
- taskList.removeElementAt(0);
- }
- return task;
- }
- /**
- * return thread pool message
- */
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append("DEFAULT_MIN_SIZE : " + DEFAULT_MIN_SIZE + "\n");
- sb.append("DEFAULT_MAX_SIZE : " + DEFAULT_MAX_SIZE + "\n");
- sb.append("DEFAULT_RELEASE_DELAY : " + DEFAULT_RELEASE_DELAY + "\n");
- sb.append("the information about your's construct ThreadPool below : \n");
- sb.append("minSize \t maxSize \t releaseDelay \n");
- sb.append(minSize + "\t" + maxSize + "\t" + releaseDelay);
- return sb.toString();
- }
- }
发表评论
-
字符编码笔记:ASCII,Unicode和UTF-8 (引用)
2009-01-07 10:39 921字符编码笔记:ASCII,Unicode和UTF-8 阮一峰 ... -
How to set up a simple LRU cache using LinkedHash
2008-11-03 18:05 1278How to set up a simple LRU cach ... -
Scalability?
2008-10-07 14:07 825严格上讲,scalability还没有正式定义, 甚至有人觉得 ... -
Cray Reminiscences
2007-08-29 15:54 789Kirk Pepperdine's attendence of ... -
lock-free
2007-06-18 22:06 9881. http://www.ibm.com/developer ... -
解决java.lang.OutOfMemoryError: PermGen space(转帖)
2007-06-05 18:07 3158解决方案就是:在启动服务器时加上指定PermGen区域的内存大 ... -
Performance...
2007-06-05 15:11 977« I used to work for... | Mai ... -
数据仓库
2007-04-18 10:38 1116... -
Expressions Transform
2007-04-13 11:13 1385Expressions, Conversion and Eva ... -
Java cleanup code
2007-04-03 12:20 1291Java shutdown hook guarantee th ... -
Java performance tunning
2007-04-03 11:37 934http://www.javaperformancetunin ... -
Running IE from command line
2007-04-03 10:58 1107Here's a simple way you can ru ... -
Unicode and UTF8
2007-04-03 10:27 907What is Unicode? Unicode provid ... -
Daemon Thread Notes
2007-04-03 09:16 26491. 只要程式中的non-Daemon thread都結束了. ... -
How to know the main class of a jar file?
2007-04-02 15:18 1026Easy. Here is an implementation ... -
The best chinese BAT tutorial(from www.boofee.net/bigfee/)
2007-03-27 11:58 1328如何创建批处理文件? 不要听了批处理文件就感到很神气 ... -
Basics - Binary search
2007-03-26 15:53 963java 代码 public class Bin ... -
MergeSort
2007-03-23 17:26 833MergeSort is a sample solutio ... -
Graph data structure
2007-03-23 12:04 8721. adjacent matrix good for bor ... -
Functional Programming For The Rest of Us
2007-03-23 10:39 1281I like connect beautiful artic ...
相关推荐
A simple C++11 Thread Pool implementation. Basic usage: // create thread pool with 4 worker threads ThreadPool pool(4); // enqueue and store future auto result = pool.enqueue([](int answer) { ...
标题中的"A simple C Thread pool implementation"表明我们将探讨一个用C语言编写的简单线程池实现。线程池的基本思想是预先创建一组线程,然后根据需要分配任务,而不是每次需要时都创建新的线程,这有助于减少线程...
标题 "A simple C++11 Thread Pool implementation.zip" 指的是一个使用C++11标准库实现的线程池示例。线程池是一种在多线程编程中管理线程资源的有效方式,它允许程序预先创建一组线程,然后根据需要分配任务,而...
这个“VC_simple-thread-pool.rar”文件看起来是一个基于Visual C++(VC)编写的简单线程池实现,用于帮助开发者更好地管理和调度线程资源。 在Windows环境下,线程池主要通过`CreateThreadpool`和相关的API函数来...
A Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLABA Simple Mesh Generator in MATLAB
"Simple C Thread Pool" 是一个开源项目,它使用C语言并基于POSIX的pthread库来实现线程池功能。在C语言中,线程池的实现相对复杂,因为C标准库并不提供原生的线程支持,而POSIX线程库(pthreads)则提供了跨多个UNIX...
在本项目"simple-pool-game-using-python"中,我们将探讨如何使用Python开发一款简单的台球游戏。这个项目是Python编程实战的一个很好的例子,它涵盖了多种关键技能,包括使用pygame库进行图形用户界面(GUI)开发、...
simple_pool 一个非常简单的池服务,用于与 mariasql 一起使用。 是为一个孤立的用例创建的,在这个用例中,在通用池中使用获取 + 排队连接并不像将它们发送到下一个连接那样优化,即使它很忙。 var simple_pool...
A simple and fast implementation of a Kalman Filter.
Simple_Tensorflow_implementation_of_Large_Scale_G_BigGAN-Tensorflow
We want to offer a short and simple MATLAB code, described in more detail than usual, so the reader can experiment (and add to the code) knowing the underlying principles. We find the node locations ...
This is a simple database engine implementation which derived from DB(2). If you true understanding this database engine, you will have more chances to be a elite in the field of computer science. ...
# ImageDeblur An simple implementation of image deblur develop environment: Ubuntu16.04 + OpenCV3.2
Simple Waypoint System和PoolManager是两个常用的插件,它们可以帮助开发者优化游戏性能,提高用户体验。 首先,Simple Waypoint System插件允许开发者轻松地为游戏对象设置路径。这个系统使得游戏中的角色或物体...
a one simple 2.49版 皮肤检测仪驱动程序/安装程序 a one simple 有两个软件版本,2.43和2.49版,一般情况下使用2.49版即可,个别购买年代久远的机子使用2.49版,如有任何疑问,请联系软件中的客服人员,谢谢 感谢...
a one simple 2.43版 皮肤检测仪驱动程序/安装程序 a one simple 有两个软件版本,2.43和2.49版,一般情况下使用2.49版即可,个别购买年代久远的机子使用2.49版,如有任何疑问,请联系软件中的客服人员,谢谢 感谢...
automata models of BZ spirals can be created by using a very simple set of equations based on a three substrate model with close connection to reaction-diffusion models, more closely resembling the ...
《简单分库中间件simple-sharding的探索与实践》 在大数据时代,数据库的扩展性和性能成为了企业系统架构的关键挑战之一。"simple-sharding",正如其名,是一款旨在简化数据库分片操作的中间件,它作为一个轻量级的...
一个很好的 单服务器 vs 多客户端模型 实例。 Console 下c++实现 其中涉及到Socket,线程池,事件。 英文的,看懂后受益匪浅。...源码解压开后: 目录test 服务器工程 目录testc 客户端工程
本文将基于《A Simple IOCP Server/Client Class》这一项目,深入探讨IOCP的基本概念、工作原理以及如何在实际编程中应用。** **一、IOCP基础** 1. **什么是IOCP** IOCP,全称Input/Output Completion Port,是一...