public Worker(SyncQueue queue) { _queue = queue; }
public void run() { while (true) { Runnable task = (Runnable) _queue.get(); task.run(); } }
protected SyncQueue _queue = null;
}
下面是一个使用这个Thread缓冲池的例子:
//构造Thread缓冲池 SyncQueue queue = new SyncQueue(10); for (int i = 0; i < 5; i ++) { new Thread(new Worker(queue)).start(); } //使用Thread缓冲池 Runnable task = new MyTask(); queue.put(task);