Terminating threads

Hi,

I have developed a simple program to test executor service and executors for thread pool creation and management. The program executes fine until I call the shudown() method. The Executor is shutdown but the threads still remain alive (verified by calling Shutdown() -> isShutdown() -> isTerminated())

I have supplied the code along with this message:

import java.util.concurrent.*;

publicclass PerfTrack{

publicstaticvoid main(String args[]){

finalint CAPACITY = 5;

final ExecutorService threadPool;

Tasks hasTasks =new Tasks();

ArrayBlockingQueue taskQueue =new ArrayBlockingQueue(CAPACITY,true);

threadPool = Executors.newFixedThreadPool(10);

threadPool.execute(new Scheduler(taskQueue));

while(hasTasks.getTasks())

threadPool.execute(new Handler(taskQueue, hasTasks));

threadPool.shutdown();

System.out.println("hello");

System.out.println(threadPool.isShutdown());

System.out.println(threadPool.isTerminated());

}

}

class Tasks{

privateboolean hasTasks;

public Tasks(){

hasTasks =true;

}

publicsynchronizedboolean getTasks(){

return hasTasks;

}

publicsynchronizedvoid setTasks(){

hasTasks =false;

}

}

class Schedulerimplements Runnable{

private ArrayBlockingQueue taskQueue;

public Scheduler(ArrayBlockingQueue taskQueue){

this.taskQueue = taskQueue;

}

privatevoid createTasks(){

for(int i = 1; i <= 20; i++){

try{

taskQueue.put(new Integer(i));

if(i == 20){

taskQueue.put(new Integer(0));

}

}

catch(InterruptedException intEx){

// Do nothing

}

}

}

publicvoid run(){

createTasks();

}

}

class Handlerimplements Runnable{

private ArrayBlockingQueue taskQueue;

private Integer queueItem;

volatile Tasks hasTasks;

public Handler(ArrayBlockingQueue taskQueue, Tasks hasTasks){

this.taskQueue = taskQueue;

this.hasTasks = hasTasks;

}

privatesynchronizedvoid getItems(){

try{

queueItem = (Integer)taskQueue.take();

if(queueItem.intValue() == 0){

hasTasks.setTasks();

}

else

System.out.println("The number is:\t" + queueItem.intValue());

}

catch(InterruptedException intEx){

//

}

}

publicvoid run(){

getItems();

}

}

Your help is greatily appreciated,

Thanks

[5694 byte] By [bce_developera] at [2007-11-26 18:28:23]
# 1

After you call

threadPool.shutdown();

you need to wait for sometime, so the tasks/threads receives the shutdown signal.

I hope you took care of the Security Permission, too. i.e. the executor of this code have permission to shutdown the created tasks.

Refer to API, again for a better explanation.

sree_at_worka at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...
# 2

Thanks for the response.

I let the program run for about five minutes to see if the threads would receive the shutdown signal but they don't seem to. I don't see why I would need to check Security Permissions for this particular example. If there was a problem wouldn't a SecurityException be thrown?

If I call the shutdownNow() method it seems to do the trick and the program terminates. Otherwise it doesn't.

bce_developera at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...
# 3

shutdown() prevents new work from being accepted by the executor but doesn't stop the processing of existing work (executing or queued). So if there is a lot of work to do then you will wait a while for the worker threads to actually terminate.

But as long as the work completes then the executor work threads should be terminating.

Try ctrl-/ (Solaris/linux) or ctrl-break (Windows) to get a thread dump during this five minute wait. Then you will be able to see where the threads are "stuck".

davidholmesa at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...
# 4
I am encountering the same problem, have you been able to figure out how to resolve this issue?
gmac75a at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...
# 5
What problem? Have you read the thread?
ejpa at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...
# 6

Java does not provide any mechanism for safely forcing a thread to stop what it is doing.[1] Instead, it provides interruption, a cooperative mechanism that lets one thread ask another to stop what it is doing.

The deprecated Thread.stop and suspend methods were an attempt to provide such a mechanism, but were quickly realized to be seriously flawed and should be avoided. See http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html for an explanation of the problems with these methods.

public class PrimeGenerator implements Runnable {

private final List<BigInteger> primes

= new ArrayList<BigInteger>();

private volatile boolean cancelled;

public void run() {

BigInteger p = BigInteger.ONE;

while (!cancelled ) {

p = p.nextProbablePrime();

synchronized (this) {

primes.add(p);

}

}

}

public void cancel() { cancelled = true; }

public synchronized List<BigInteger> get() {

return new ArrayList<BigInteger>(primes);

}

}

kilyasa at 2007-7-9 6:02:35 > top of Java-index,Core,Core APIs...