Reputation: 1830
I have a thread that runs a Runnable and looks like:
Runnable run = new Runnable() {
@Override
public void run() {
functionCall();
}
};
Thread thread = new Thread(run);
thread.start();
void functionCall() {
for (int i=0; i<1000000; i++) System.out.println(i);
}
And I would like to stop it no matter what. I've tried with thread.interrupt()
but I haven't been able to make it work because the functionCall()
method doesn't really depends on me (I have no idea what's inside that function, this is only an example).
As far as I have seen, everyone uses a volatile boolean variable to check it but this thread is only executed ONCE so it's not really what I need.
Upvotes: 2
Views: 123
Reputation: 272227
Thread.interrupt()
requires the thread being called on to yield control to the OS (for IO access, sleep etc.). So in order to use this you need to ensure that your thread does this (i.e. isn't solely computationally). See this JavaSpecialists entry for more information.
Upvotes: 2