📄 threadpool.java
字号:
//ThreadPool.javapackage com.wrox.threadpool;import java.util.*;public class ThreadPool { private Vector freeThreads = new Vector(); private Vector inUseThreads = new Vector(); private static int INITIAL_SIZE = 10; public ThreadPool() { fillPool(INITIAL_SIZE); } private void fillPool(int poolSize) { for (int i = 0; i < poolSize; i++) { PoolableThread pt = new PoolableThread(this); pt.start(); freeThreads.add(pt); } try { Thread.sleep(2000); } catch (InterruptedException ie) {} } /** * Allocates a thread and starts this Runnable task. * @param task the task to run. */ public synchronized void runTask(Runnable task) { if (freeThreads.isEmpty()) { throw new RuntimeException("All threads are in use"); } PoolableThread t = (PoolableThread) freeThreads.remove(0); inUseThreads.add(t); t.setTask(task); } synchronized void free(PoolableThread t) { inUseThreads.remove(t); freeThreads.add(t); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -