My program is calling the mathematical solver CPLEX multiple times. There is the possibility of setting a time limit for each time CPLEX is being executed from java, which I am using. The problem is that in some rare instances my program gets just stuck inside the .solve()
method provided by CPLEX. I don't know where that fault lies, it is definitely not in the mathematical model I am solving, because exporting it and solving it outside of java works. But this is not what my question is about, although of course I'd be thankful for your input on that, too.
My question is: How can I make my program leave the .solve()
method immediately no matter what? In the example below I tried to fix this problem. There, the .solve()
method is called in the method execute()
.
public synchronized void executeSafely(int limitSeconds) {
Thread t = new Thread() {
@Override
public void run() {
execute();
}
};
t.start();
try {
Thread.sleep(3000 * limitSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
t.interrupt();
}
This approach does not work because there is no check for interruption inside the .solve()
method and I cannot change that method. Replacing .interrupt()
with .stop()
does not work either since this leads to the suspension of the thread because of exception ThreadDeath
.
So is there any other way of immediately terminating the execution of an external program?