Depending on what the other thread is doing, I'd suggest interrupts or having a boolean value set (and checked in the other thread). If execution of the other thread can be stopped at any time use an interrupt, however if execution cannot be stopped after a certain point in time, simply set/check a boolean before that code is entered.
Add a boolean to your stopwatch class:
private static boolean continue = true;
Create a method to check the boolean:
public static boolean shouldContinue() {
return this.continue;
}
Modify your setInterval()
to change the boolean:
private static final int setInterval() {
if (interval == 1)
continue = false;
return --interval;
}
Add the check somewhere in your other class:
if (!(Stopwatch.shouldContinue())) {
return;
}
or
if (Stopwatch.shouldContinue()) {
//do work here
}
Since it sounds like my comments helped, I thought I'd make it into an answer so we can remove this from the unanswered list.