A
A
artshelom2016-11-02 13:51:07
Java
artshelom, 2016-11-02 13:51:07

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());
    }
}

How can I stop a thread from executing just by referring to its name??
Here in such code??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2016-11-02
@artshelom

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 question

Ask a Question

731 491 924 answers to any question