為什么要建立線程池?
在多線程項目中,如果建立的線程過多,反而可能導致運行速度大大減慢,這是由于線程建立所花費的時間和資源都比較多。
所以我們在多線程中必須很好地來管理線程, 在很好利用多線程能“同步工作”的好處之外,更有效地提高程序運行速度。
線程池是什么?
線程池是指具有固定數量的線程組成的一種組件。這些線程用來循環執行多個應用邏輯。
怎么建立線程池?
線程池主要包括4個部分,它們是:
1. 線程管理
主要是用來建立,啟動,銷毀工作線程和把工作任務加入工作線程。
2. 工作線程
它是真正的線程類,運行工作任務。
3. 工作隊列
它是用來封裝線程的容器。
4. 工作任務
它是實現應用邏輯的具體類。
線程管理類:
- <SPAN style="COLOR: #3366ff">import java.util.ArrayList;
- import java.util.List;
- import java.util.Queue;
- import java.util.concurrent.ConcurrentLinkedQueue;
- /**
- * ThreadPoolManager.java
- *
- * Copyright (C) 2008 State Street Corporation. All Rights Reserved.
- *
- */
- /**
- * the thread pool manager, is responsible for starting and stopping the work thread.
- *
- * @author e458487
- * @version 1.0
- */
- 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();
- }
- }
- }</SPAN>
工作線程類:
- <SPAN style="COLOR: #3366ff">import java.util.Queue;
- /**
- * WorkThread.java
- *
- * Copyright (C) 2008 State Street Corporation. All Rights Reserved.
- *
- */
- /**
- * the work thread used pull the task of task queue, and execute it.
- *
- * @author e458487
- * @version 1.0
- */
- 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;
- }
- }</SPAN>
工作任務接口:
- <SPAN style="COLOR: #3366ff">/**
- * Task.java
- *
- * Copyright (C) 2008 State Street Corporation. All Rights Reserved.
- *
- */
- /**
- * The task want to execute.
- *
- * @author e458487
- * @version 1.0
- */
- public interface Task {
- public void execute();
- }</SPAN>
工作任務類:
- <SPAN style="COLOR: #3366ff">/**
- * SimpleTask.java
- *
- * Copyright (C) 2008 State Street Corporation. All Rights Reserved.
- *
- */
- /**
- * @author e458487
- * @version 1.0
- */
- public class SimpleTask implements Task {
- /* (non-Javadoc)
- * @see Task#execute()
- */
- public void execute() {
- System.out.println(Thread.currentThread());
- }
- }</SPAN>
線程池測試類:
- <SPAN style="COLOR: #3366ff">/**
- * ThreadPoolDemo.java
- *
- * Copyright (C) 2008 State Street Corporation. All Rights Reserved.
- *
- */
- /**
- * @author e458487
- * @version 1.0
- */
- 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();
- }
- }</SPAN>
http://www.javaeye.com/topic/387566