How can I kill a Thread

0

I would like to know how to kill a thread.

public class thread {

    private static void metodo(){
        new Thread(){
            @Override
            public void run(){
                while(true){
                    System.out.println("Executando...");
                    try {
                        Thread.sleep(2000);
                        Thread.interrupted();
                    } catch (InterruptedException ex) {
                        System.out.println("Interrompido!");
                        break;
                    }
                }
            }
        }.start();
    }

    public static void main(String[] args) {
        metodo();
    }

}

I would not like to give a simple break in the While, I would like to kill Thread with methods of itself.

I have an application that needs to stop a Thread to run it again, without running the same method in parallel, so my question is.

    
asked by anonymous 14.04.2018 / 18:04

1 answer

1

Thread does not kill itself, it has to stop of its own accord, so you can not escape using a sign or flag mechanism.

    
14.04.2018 / 19:42