自创java线程池


/**
* 2010-8-3 下午09:14:29
* @auth cjj
*/
package org.prime.threadpool;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @author cjj
*
*/
public class CPrimePool {

private Object lock = new Object();

private volatile boolean stop = false;

private int coreSize;

private int maxSize;

private AtomicInteger curThreadCount = new AtomicInteger();

private Queue<WorkThread> cacheThreads = new ConcurrentLinkedQueue<WorkThread>();

public CPrimePool() {
init();
}

public CPrimePool(int coreSize, int maxSize) {
this.coreSize = coreSize;
this.maxSize = maxSize;
init();
}

private void init() {
if (coreSize == 0) {
coreSize = 1;
}
if (maxSize == 0) {
maxSize = Runtime.getRuntime().availableProcessors() + 1;
}
}

public void runTask(Runnable task) {
if (!stop) {
if (curThreadCount.get() < coreSize) {
WorkThread work = new WorkThread();
int i = curThreadCount.getAndIncrement();
work.setName("Work Thread : "+i);
work.start();
work.setTask(task);
} else {
WorkThread work = cacheThreads.poll();
if (work != null) {
work.setTask(task);
} else {
if (curThreadCount.get() < maxSize) {
work = new WorkThread();
int i = curThreadCount.getAndIncrement();
work.setName("Work Thread : "+i);
work.start();
work.setTask(task);
} else {
synchronized (lock) {
while ((work = cacheThreads.poll()) == null
&& !stop) {
try {
lock.wait();
} catch (InterruptedException e) {
// ignore
}
}
}
if (!stop) {
work.setTask(task);
}
}
}
}
}
}

public void stop() {
stop = true;
WorkThread work = null;
while ((work = cacheThreads.poll()) != null) {
work.interrupt();
}
}

private class WorkThread extends Thread {

private Runnable task;

public void setTask(Runnable task) {
synchronized (this) {
this.task = task;
notifyAll();
}

}

@Override
public void run() {
while (!stop) {
try {
if (task != null) {
task.run();
}
synchronized (lock) {
task = null;
// 回收
cacheThreads.add(this);

lock.notifyAll();
}
synchronized (this) {
while (task == null && !stop) {
wait();
}
}
} catch (InterruptedException ie) {
break;
}
}
}

}

}


使用示例代码:

/**
* 2010-8-3 下午11:31:28
* @auth cjj
*/
package org.prime.threadpool.test;

import org.prime.threadpool.CPrimePool;

/**
* @author cjj
*
*/
public class Test {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
CPrimePool pool = new CPrimePool();
for (int i = 0; i < 500000; i++) {
pool.runTask(new MyThread(i));
}
// Thread.sleep(30000);
pool.stop();
}

static class MyThread implements Runnable {

private int i;

public MyThread(int i) {
this.i = i;
}

@Override
public void run() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
System.out.println("thread:" + i);
}
}

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值