I have a consumer/producer program where:
- items to be processed (Strings) go into a BlockingQueue
- I instantiate a fixed number of threads that perform take() from that BlockingQueue and then process those Strings.
The processing threads are instances of this class:
class ProcessingThread extends Thread {
private void process(String s) {
(big method with calls to other big methods)
}
public void run() {
String s = queue.take();
process(s);
}
}
Converting to an Executor would be quite straightforward: the Strings would go straight into the Executor, along with a Runnable:
class Task implements Runnable {
private void process(String s) {
(big method with calls to other big methods)
}
public void run() {
process(s);
}
}
and then:
executor.execute(new Task(s));
But...every time I instantiate a Task, it would instantiate a very large chunk of code (the processor) - I can't have that, memory requirements would go sky high.
How do I do this?