I have a thread pool which creates workers and the workers take the jobs from a BlockingQueue
.
The threads wait on take()
from the queue.
Even on explicitly calling the thread interrupt method for the running threads, they are still waiting on take()
. What is right way of dealing with blockingqueue
public class ThreadPoolGen {
static final Logger LOG = Logger.getLogger(ThreadPoolGen.class);
private LinkedBlockingQueue<Runnable> queue;
private int threadCount;
private Worker[] workers;
private Thread[] workerThreads;
public ThreadPoolGen(int count) throws CountException{
if(isValidCount(count))
this.threadCount = count;
else
throw new CountException("Invalid Thread Count");
workers = new Worker[count];
workerThreads = new Thread[count];
queue = new LinkedBlockingQueue<Runnable>();
startThreads();
}
public boolean execute(Runnable task){
return queue.offer(task);
}
private void startThreads(){
synchronized (this) {
for(int i=0;i<threadCount;i++){
workers[i] = new Worker();
workerThreads[i] = new Thread(workers[i]);
workerThreads[i].start();
}
}
}
public boolean shutDown(){
try{
for(Worker w: workers){
w.thread.interrupt();
}
queue.clear();
for(Thread workerThread : workerThreads){
workerThread.interrupt();
}
return true;
}catch(Exception e){
LOG.debug(Thread.currentThread()+": Worker Thread Shutdown Failed");
return false;
}
}
private boolean isValidCount(int count){
if(count<Integer.MAX_VALUE && count>0)
return true;
else
return false;
}
private class Worker implements Runnable{
final Thread thread;
private Worker(){
this.thread = Thread.currentThread();
}
@Override
public void run() {
try{
while(true){
try{
Runnable r = queue.take();
r.run();
}catch(InterruptedException interrupt){
LOG.debug("Interrupted exception in: "+thread.getName());
}
}
}catch(Exception intr){
this.thread.interrupt();
}finally{
this.thread.interrupt();
}
}
}
}
The calling class :
public class Runner {
public static void main(String[] args) {
try {
System.out.println("BeforeLaunch");
ThreadPoolGen gen = new ThreadPoolGen(10);
gen.execute(new Runnable() {
@Override
public void run() {
System.out.println("Inside Runnable");
}
});
gen.shutDown();
} catch (CountException ce) {
} catch (Exception e) {
}
}
}