Answer the question
In order to leave comments, you need to log in
How to stop parallel execution?
public class qw {
public static void main(String[] args) {
System.out.println("Начало главной нити");
for (int i=0;i<7;i++){
new ww("id"+i).start();
}
System.out.println("Окончила работу главная нить");
}
}
class ww extends Thread{
public ww(String nazv) {
super(nazv);
}
@Override
public void run() {
System.out.println("Поток %s начал работу... \n"+ Thread.currentThread().getName());
try{
Thread.sleep(5000);
}
catch(InterruptedException e){
System.out.println("Поток прерван");
}
System.out.println("Поток %s завершил работу... \n"+ Thread.currentThread().getName());
}
}
Answer the question
In order to leave comments, you need to log in
The thread must have a wrapper that will have a boolean property ( interruptionRequested
, for example) that the thread needs to check on its own between atomic operations of its payload. (" Copied file, checked - false
, copied next, checked - false
, copied another one, checked - true
, finished ").
That is, the thread itself decides when it will stop, but we can signal it about it. Otherwise, we risk breaking the data, because we will not know for sure what state they are in.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question