线程管理类:使用入口
public class ThreadPoolManager {
private static final int DEFAULT_POOL_SIZE = 4;
private List<WorkThread> threadPool;
private Queue<Task> taskQueue;
private int poolSize;
public ThreadPoolManager() {
this(DEFAULT_POOL_SIZE);
}
public ThreadPoolManager(int poolSize) {
if(poolSize <= 0) {
this.poolSize = DEFAULT_POOL_SIZE;
}else {
this.poolSize = poolSize;
}
threadPool = new ArrayList<WorkThread>(this.poolSize);
taskQueue = new ConcurrentLinkedQueue<Task>();
startup();
}
public void startup() {
System.out.println("start work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
WorkThread workThread = new WorkThread(taskQueue);
threadPool.add(workThread);
workThread.start();
}
}
}
public void shutdown() {
System.out.println("shutdown work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
threadPool.get(i).shutdown();
}
System.out.println("done...");
}
}
public void addTask(Task task) {
synchronized(taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
}
工作线程类:
public class WorkThread extends Thread {
private boolean shutdown = false;
private Queue<Task> queue;
public WorkThread(Queue<Task> queue) {
this.queue = queue;
}
public void run() {
while(!shutdown) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread() + " is running...");
synchronized(queue) {
if(!queue.isEmpty()) {
Task task = queue.poll();
task.execute();
}else {
try {
queue.wait(1000);
System.out.println(Thread.currentThread() + " wait...");
}catch(InterruptedException e) {
}
}
}
}
}
public void shutdown() {
shutdown = true;
}
}
工作任务接口:
public interface Task {
public void execute();
}
工作任务类:
class SimpleTask implements Task {
/* (non-Javadoc)
* @see Task#execute()
*/
public void execute() {
System.out.println(Thread.currentThread());
}
}
线程池测试类:
public class ThreadPoolDemo {
public static void main(String[] args) {
ThreadPoolManager threadMg = new ThreadPoolManager();
for(int i = 0; i < 50; i++) {
threadMg.addTask(new SimpleTask());
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadMg.shutdown();
}
}
public class ThreadPoolManager {
private static final int DEFAULT_POOL_SIZE = 4;
private List<WorkThread> threadPool;
private Queue<Task> taskQueue;
private int poolSize;
public ThreadPoolManager() {
this(DEFAULT_POOL_SIZE);
}
public ThreadPoolManager(int poolSize) {
if(poolSize <= 0) {
this.poolSize = DEFAULT_POOL_SIZE;
}else {
this.poolSize = poolSize;
}
threadPool = new ArrayList<WorkThread>(this.poolSize);
taskQueue = new ConcurrentLinkedQueue<Task>();
startup();
}
public void startup() {
System.out.println("start work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
WorkThread workThread = new WorkThread(taskQueue);
threadPool.add(workThread);
workThread.start();
}
}
}
public void shutdown() {
System.out.println("shutdown work thread...");
synchronized(taskQueue) {
for(int i = 0; i < this.poolSize; i++) {
threadPool.get(i).shutdown();
}
System.out.println("done...");
}
}
public void addTask(Task task) {
synchronized(taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
}
工作线程类:
public class WorkThread extends Thread {
private boolean shutdown = false;
private Queue<Task> queue;
public WorkThread(Queue<Task> queue) {
this.queue = queue;
}
public void run() {
while(!shutdown) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
System.out.println(Thread.currentThread() + " is running...");
synchronized(queue) {
if(!queue.isEmpty()) {
Task task = queue.poll();
task.execute();
}else {
try {
queue.wait(1000);
System.out.println(Thread.currentThread() + " wait...");
}catch(InterruptedException e) {
}
}
}
}
}
public void shutdown() {
shutdown = true;
}
}
工作任务接口:
public interface Task {
public void execute();
}
工作任务类:
class SimpleTask implements Task {
/* (non-Javadoc)
* @see Task#execute()
*/
public void execute() {
System.out.println(Thread.currentThread());
}
}
线程池测试类:
public class ThreadPoolDemo {
public static void main(String[] args) {
ThreadPoolManager threadMg = new ThreadPoolManager();
for(int i = 0; i < 50; i++) {
threadMg.addTask(new SimpleTask());
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadMg.shutdown();
}
}