You shouldn't create threads by yourself when using a threadpool. Instead of WorkerThread
class you should use a class that implements Runnable
but is not a thread. Passing a Thread
object to the threadpool won't make the thread run actually. The object will be passed to a different internal thread, which will simply execute the run
method of your WorkerThread
class.
The ExecutorService
is simply incompatible with the way you want to write your program.
In the code you have right now, these WorkerThread
s will stop to work when your ListWrapper
is empty. If you then add something to the list, nothing will happen. This is definitely not what you wanted.
You should get rid of ListWrapper
and simply put your tasks directly into the threadpool. The threadpool already incorporates an internal list of jobs shared between the threads. You should just submit your jobs to the threadpool and it will handle them accordingly.
To answer your questions:
1) Which value ('x' or 'NTHREADS') should I set to control the number of concurrent threads? Or it doesn't matter in either I choose?
NTHREADS, the threadpool will create the necessary number of threads.
2) How is this approach different from simply using the Producer-Consumer pattern --creating a fixed number of 'stud' threads to execute the tasks(shown in the code below)?
It's just that ExecutorService
automates a lot of things for you. You can choose from a lot of different implementations of threadpools and you can substitute them easily. You can use for instance a scheduled executor. You get extra functionality. Why reinvent the wheel?