I wanted to ask if it is possible for a thread to end itself. I have a method in an instance of a thread that checks if an int Overflows but its not the run() method. I want to stop the thread if the internalSteps reach Integer.MAX_VALUE / numberOfAllThreads
private void addInternalStep() {
++internalSteps;
if( internalSteps == (Integer.MAX_VALUE / numberOfAllThreads) ) {
System.out.println("Thread stopped. In danger of overflow.");
this.interrupt(); // Not stopping the thread
}
}
@Override
public void run() {
while( !isInterrupted() ) {
if(Math.pow(Math.random(), 2) + Math.pow(Math.random(), 2) < 1) {
hits = hits.add(BigDecimal.ONE);
}
counter = counter.add(BigDecimal.ONE);
addInternalStep();
}
}
Why isnt this.interrupt() working ? And how do I interrupt the thread in the instance of itsself ?