線程池通俗的描述就是預先創建若干空閒線程
本文旨在使用Java語言編寫一個通用的線程池
//ThreadTask
package polarman
/** *//**
*線程任務
* @author ryang
*
*/
public interface ThreadTask
public void run();
}
//PooledThread
package polarman
import java
/** *//**
*接受線程池管理的線程
* @author ryang
*
*/
public class PooledThread extends Thread
protected Vector tasks = new Vector();
protected boolean running = false;
protected boolean stopped = false;
protected boolean paused = false;
protected boolean killed = false;
private ThreadPool pool;
public PooledThread(ThreadPool pool)
}
public void putTask(ThreadTask task)
}
public void putTasks(ThreadTask[] tasks)
}
public void putTasks(Collection tasks)
}
protected ThreadTask popTask()
else
return null;
}
public boolean isRunning()
return running;
}
public void stopTasks()
stopped = true;
}
public void stopTasksSync()
stopTasks();
while(isRunning())
sleep(
} catch (InterruptedException e)
}
}
}
public void pauseTasks()
paused = true;
}
public void pauseTasksSync()
pauseTasks();
while(isRunning())
sleep(
} catch (InterruptedException e)
}
}
}
public void kill()
interrupt();
else
killed = true;
}
public void killSync()
kill();
while(isAlive())
sleep(
} catch (InterruptedException e)
}
}
}
public synchronized void startTasks()
running = true;
this
}
public synchronized void run()
ThreadTask task;
while((task = popTask()) != null)
stopped = false;
if(tasks
break;
}
}
if(paused)
paused = false;
if(tasks
break;
}
}
}
running = false;
}
if(killed)
killed = false;
break;
}
}
}catch(InterruptedException e)
return;
}
//System
}
}
//ThreadPool
package polarman
import java
/** *//**
*線程池
* @author ryang
*
*/
public class ThreadPool
protected int maxPoolSize;
protected int initPoolSize;
protected Vector threads = new Vector();
protected boolean initialized = false;
protected boolean hasIdleThread = false;
public ThreadPool(int maxPoolSize
}
public void init()
initialized = true;
for(int i=
PooledThread thread = new PooledThread(this);
thread
}
//System
}
public void setMaxPoolSize(int maxPoolSize)
if(maxPoolSize < getPoolSize())
setPoolSize(maxPoolSize);
}
/** *//**
*重設當前線程數
* 若需殺掉某線程
* @param size
*/
public void setPoolSize(int size)
initPoolSize = size;
return;
}else if(size > getPoolSize())
PooledThread thread = new PooledThread(this);
thread
}
}else if(size < getPoolSize())
}
}
//System
}
public int getPoolSize()
}
protected void notifyForIdleThread()
hasIdleThread = true;
}
protected boolean waitForIdleThread()
hasIdleThread = false;
while(!hasIdleThread && getPoolSize() >= maxPoolSize)
return false;
}
}
return true;
}
public synchronized PooledThread getIdleThread()
return th;
}
if(getPoolSize() < maxPoolSize)
PooledThread thread = new PooledThread(this);
thread
return thread;
}
//System
if(waitForIdleThread() == false)
return null;
}
}
public void processTask(ThreadTask task)
PooledThread th = getIdleThread();
if(th != null)
}
}
public void processTasksInSingleThread(ThreadTask[] tasks)
PooledThread th = getIdleThread();
if(th != null)
}
}
public void processTasksInSingleThread(Collection tasks)
PooledThread th = getIdleThread();
if(th != null)
}
}
}
下面是線程池的測試程序
//ThreadPoolTest
import java
import polarman
public class ThreadPoolTest
public static void main(String[] args)
final ThreadPool pool = new ThreadPool(
Thread cmdThread = new Thread()
BufferedReader reader = new BufferedReader(new InputStreamReader(System
while(true)
}
}else if(words[
}
}else if(words[
}
}
} catch (IOException e)
}
}
}
};
cmdThread
/**//*
for(int i=
SimpleTask task = new SimpleTask(
}*/
}
}
class SimpleTask implements ThreadTask
private String taskName;
private int timeLen;
public SimpleTask(String taskName
}
public void run()
try
}
System
}
}
使用此線程池相當簡單
ThreadPool pool = new ThreadPool(
要處理的任務實現ThreadTask
兩行代碼即可調用
ThreadTask task =
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27203.html