1

This question is the culmination of a few questions leading up to it. I just want to make sure I have everything right before I commit any changes that I've made.

So here's what I'm working with for data structure:

In an abstract class:

public abstract void doRun();

public void run(){
    try{
    while(!Thread.currentThread().isInterrupted() || hasRunBefore){
        doRun();
    }
    }catch (InterruptedException e){Thread.sleep(1); System.out.println("Thread Interrupted: "); e.printStackTrace(); }

}

Then in the child classes that are implementing my abstract class:

public void doRun(){
    doChildClassStuff();
}

Then when calling these in my main class I simply do

 ClassName myClass = new ClassName(constructor.list);
 ExecutorService threadPool = Executors.newFixedThreadPool(12);
    List<Future<?>> taskList = new ArrayList<Future<?>>();
    taskList.add(myClass);
    for(Future future : taskList){
        try{
            future.get(1, TimeUnit.SECONDS);
        }catch(CancellationException cx){ System.err.println("Cancellation Exception: "); cx.printStackTrace();
        }catch(ExecutionException ex){ System.err.println("Execution Exception: ");ex.printStackTrace();
        }catch(InterruptedException ix){ System.err.println("Interrupted Exception: ");ix.printStackTrace();
        }catch(TimeoutException ex) {future.cancel(true);}
    }
    threadPool.shutdown();
    threadPool.awaitTermination(60, TimeUnit.SECONDS);
    threadPool.shutdownNow();

and if thread.sleep() fails, that means that the thread has been interrupted and should exit. If all the threads take over 60 seconds then they will all be sent thread.interrupt() commands from the awaitTermination, right?

Am I completely off-base here?

A_Elric
  • 3,508
  • 13
  • 52
  • 85

1 Answers1

1

It looks like you are trying to handle Thread interrupts in the base class. Unfortunately, there is no way for the base class to "listen" for thread interrupts. Your doChildClassStuff() needs to handle the Thread.currentThread().isInterrupted() checks itself. It needs to be able to detect if the thread has been interrupted so it can return or throw an InterruptedException.

You don't need the Thread.sleep(1);. It will throw an InterruptedException if the thread has been interrupted but you can test for that with Thread.currentThread().isInterrupted() easily. Since you are in a catch block for InterruptedException already, I'm not exactly sure what it's point is anyway. Also, your doRun() does not throw InterruptedException so that code sample (as it stands) won't compile.

Gray
  • 115,027
  • 24
  • 293
  • 354
  • Is there really no way that I can implement a listener to the parent? There's about 50 classes that I would have to hand modify and that would mean a TON of commits – A_Elric Oct 18 '12 at 16:53
  • Unfortunately, no @Damien.Bell. There is no way for the base class to "listen" for thread interrupts. – Gray Oct 18 '12 at 16:58
  • Crap, well... this also presents another issue in my child classes, they aren't made to run in loops, they are essentially simple statements that execute a few runnable methods each. – A_Elric Oct 18 '12 at 16:58
  • And some of these methods take a long time to run? The question is how to cancel them? We'd talked about setting timeout values on remote connections. Another thing to consider is to use NIO InterruptibleChannel where you can. HttpClient, for example, can be injected with a Socket factory which can do that. See here: http://stackoverflow.com/a/9041770/179850 – Gray Oct 18 '12 at 17:01
  • No easy answer unfortunately @Damien.Bell. :-| – Gray Oct 18 '12 at 17:02
  • I feel like using a Thread.stop() is my only way to handle this then. The structure is simply not going to work otherwise...Is there a really solid reason not to use it? – A_Elric Oct 18 '12 at 17:05
  • I think it's worth a try @Damien.Bell. It has been deprecated for a reason but you may find in practice that it works ok. Just re-read the deprecated comments before you jump: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#stop() – Gray Oct 18 '12 at 17:24