I am trying to interrupt thread that is working long. In my example inside run() method appear lots of different method with take quite a lot of time (e.g writing into database). I would like to stop (kill) that thread from another one. I have seen solution like:
while(!Thread.currentThread().isInterrupted()){
try{
// do stuff
}catch(InterruptedException e){
Thread.currentThread().interrupt(); // propagate interrupt
}
}
but I've got no while loop inside my code. Can anyone help me to deal with that?
Here's the code:
public void run() //parsingThread
{
try {
for(int index=0; index<tasks.size();)
{ //do sth
if(Thread.currentThread().isInterrupted()==true)
{
System.out.println("Parsing ABORTED");
Thread.currentThread().interrupt();
}
}
for(int index=0; index<1000;index++)
{ //do sth
if(Thread.currentThread().isInterrupted()==true)
{
System.out.println("Parsing ABORTED");
Thread.currentThread().interrupt();
}
}}