当前位置: 技术问答>java相关
怎么写一个有main的类来测试这段线程池的代码
来源: 互联网 发布时间:2015-11-10
本文导语: 我在java中文网下载了一个java线程池的例子,但是我不能运行,于是我写了一个类 public class CallThreadPool { //public static void main(string argv[]) public static void main(String[] args) { Runnable runner=new Runnable(); //此句...
我在java中文网下载了一个java线程池的例子,但是我不能运行,于是我写了一个类
public class CallThreadPool {
//public static void main(string argv[])
public static void main(String[] args)
{
Runnable runner=new Runnable(); //此句出错
ThreadPool my=new ThreadPool("mypool",5,10);
my.run(runner); //此句出错
}
run(Runnable runner) 方法中的runner从哪来的,有什么用?
线程池代码如下
/**
* free software
* from apusic
* by www.cn-java.com 2001
*/
import java.util.LinkedList;
public class ThreadPool
{
static final long IDLE_TIMEOUT = 60000L;
private String name;
private int minsize;
private int maxsize;
private int nextWorkerId = 0;
private LinkedList pool = new LinkedList();
public ThreadPool() {
this("PooledThread");
}
public ThreadPool(String name) {
this(name, 0, 20);
}
public ThreadPool(String name, int minsize, int maxsize) {
this.name = name;
this.minsize = minsize;
this.maxsize = maxsize;
}
public synchronized void run(Runnable runner) {
Worker worker;
if (runner == null) {
throw new NullPointerException();
}
// get a worker from free list...
if (!pool.isEmpty()) {
worker = (Worker) pool.removeFirst();
} else {
// ...no free worker available, create new one...
worker = new Worker(name + "-" + ++nextWorkerId);
worker.start();
}
// ...and wake up worker to service incoming runner
worker.wakeup(runner);
}
// Notified when a worker has idled timeout
// @return true if worker should die, false otherwise
synchronized boolean notifyTimeout(Worker worker) {
if (worker.runner != null) {
return false;
}
if (pool.size() > minsize) {
// Remove from free list
pool.remove(worker);
return true; // die
}
return false; // continue
}
// Notified when a worker has finished his work and
// free to service next runner
// @return true if worker should die, false otherwise
synchronized boolean notifyFree(Worker worker) {
if (pool.size()
public class CallThreadPool {
//public static void main(string argv[])
public static void main(String[] args)
{
Runnable runner=new Runnable(); //此句出错
ThreadPool my=new ThreadPool("mypool",5,10);
my.run(runner); //此句出错
}
run(Runnable runner) 方法中的runner从哪来的,有什么用?
线程池代码如下
/**
* free software
* from apusic
* by www.cn-java.com 2001
*/
import java.util.LinkedList;
public class ThreadPool
{
static final long IDLE_TIMEOUT = 60000L;
private String name;
private int minsize;
private int maxsize;
private int nextWorkerId = 0;
private LinkedList pool = new LinkedList();
public ThreadPool() {
this("PooledThread");
}
public ThreadPool(String name) {
this(name, 0, 20);
}
public ThreadPool(String name, int minsize, int maxsize) {
this.name = name;
this.minsize = minsize;
this.maxsize = maxsize;
}
public synchronized void run(Runnable runner) {
Worker worker;
if (runner == null) {
throw new NullPointerException();
}
// get a worker from free list...
if (!pool.isEmpty()) {
worker = (Worker) pool.removeFirst();
} else {
// ...no free worker available, create new one...
worker = new Worker(name + "-" + ++nextWorkerId);
worker.start();
}
// ...and wake up worker to service incoming runner
worker.wakeup(runner);
}
// Notified when a worker has idled timeout
// @return true if worker should die, false otherwise
synchronized boolean notifyTimeout(Worker worker) {
if (worker.runner != null) {
return false;
}
if (pool.size() > minsize) {
// Remove from free list
pool.remove(worker);
return true; // die
}
return false; // continue
}
// Notified when a worker has finished his work and
// free to service next runner
// @return true if worker should die, false otherwise
synchronized boolean notifyFree(Worker worker) {
if (pool.size()