I know that the title seems deprecated, but believe me, it is not. In our JAVA application, we are using an SDK to handle a connection with a device. The problem is, the close function sometimes (probable due to a hardware error) freezes and causes the whole app to freeze and no longer be responsive to the user. Note that I don't have access to this function implementation (as it is inside a DLL), therefore I can't put a flag inside it to check. I have tried to put it inside a thread then tried to kill it but I couldn't, I tried all the approaches mentioned here How to Stop Execution After a Certain Time in Java And this is how I implemented one of these approaches:
Runnable runnable =new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().interrupted())
{
System.out.println("this is printed");
theFunctionFromTheDllFile();
System.out.println("this is not printed");
}
}
};
Thread thread = new Thread(runnable);
thread.start();
thread.join(3*1000);
if (thread.isAlive()) {
thread.interrupt();
}
So is there any solution that I can use to stop this function regardless of its result after about 5 seconds for example?