Search code examples
javamultithreadingsoot

Add timeout behavior to a class using an old framework


I am using the SOOT framework for some static analysis. However, sometimes the analysis takes hours and I want to set a timeout to stop analysis and continue it with another program.

To achieve that kind of behavior I run the analysis in a thread:

thread = new Thread(new Runnable() { run(){ buildCallgraph(); } )};

!! The buildCallgraph() method is provided by SOOT and I have no chance to change it. !!

Currently I have no idea how to stop the thread. Interrupt won't work since buildCallgraph() is never checking if it's thread has been interrupted. Thread.stop() shouldn't be used.

So, does anyone has an idea how to terminate the Thread?

Regards Robert


Solution

  • There are good reasons for not using Thread.stop(), so how about a different approach: Instead of running the analysis in a separate thread, try running it as a separate program.

    It'll mean a small performance hit, but pragmatically, task creation and serializing/deserializing the results shouldn't have a measurable effect compared to the scale of your current timing.

    Unlike Thread.stop(), killing a program results in a clean exit since the jvm exits.

    The downside to this is that the answers to your followup questions ("how do I start a task from within a java program", "how do I wait for the completion of another program", and "how do I programmatically kill another program") are all specific to operating system.

    The upside is that they're already answered:

    1. How to start an external program inside a java program
    2. Java: wait for exec process till it exits
    3. Java tool/method to force-kill a child process