Re: An Executor-like structure providing more than threads
On Sat, 16 Jan 2010, Roedy Green wrote:
On Sun, 17 Jan 2010 01:11:36 +0000, Tom Anderson
<twic@urchin.earth.li> wrote, quoted or indirectly quoted someone who
said :
Any thoughts? What's the right way to do this?
Does Executor dispense just raw Threads or you can you get it to
dispense your custom Thread class? If so, your thread constructor could
allocate the extra goodies.
I can, and that's exactly what it does:
ExecutorService executor = Executors.newFixedThreadPool(numThreads);
((ThreadPoolExecutor)executor).setThreadFactory(new DownloadThreadFactory());
public class DownloadThreadFactory implements ThreadFactory {
HttpConnectionManager connMgr = new MultiThreadedHttpConnectionManager();
public Thread newThread(Runnable r) {
return new DownloadThread(connMgr, r);
}
}
public class DownloadThread extends Thread {
private final HttpClient client;
private byte[] buffer;
public DownloadThread(HttpConnectionManager connMgr, Runnable r) {
super(r);
client = new HttpClient(connMgr);
buffer = new byte[1024];
}
}
tom
--
Why did one straw break the camel's back? Here's the secret: the million
other straws underneath it - it's all mathematics. -- Mos Def