Решил поиграться немного с прерыванием потоков:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package ru.yamakarov.interrupt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InterruptExample {

    public static void main(String[] args) {
        Thread a = new Thread(() -> {
            while (true) {
                // Do some work.
                if (Thread.currentThread().isInterrupted()) { // Checks interruption flag without clearing it.
                    System.out.println("Thread a was interrupted");
                    return;
                }
            }
        });
        a.start();

        Thread b = new Thread(() -> {
            while (true) {
                // Do some work.

                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    // When you catch InterruptedException, you must set up interruption flag or rethrow exception.
                    Thread.currentThread().interrupt();
                    e.printStackTrace();
                    return;
                }
            }
        });
        b.start();

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
            while (true) {
                System.out.println("Enter thread to interrupt:");

                String s = reader.readLine();
                switch (s) {
                    case "a":
                        a.interrupt();
                        break;
                    case "b":
                        b.interrupt();
                        break;
                }
                if ("exit".equals(s)) {
                    break;
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            a.join();
            b.join();
            // Always false, because interruption flag is cleared when the thread finishes.
            System.out.println(a.isInterrupted());
            System.out.println(b.isInterrupted());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

Я проверил 2 способа прервать поток. С помощью Thread.sleep(0) и ловлей исключения. И через проверку флага Thread.currentThread().isInterrupted().

Первый способ кажется более предпочтительным в случае работы нескольких потоков. Поток будет давать возможность работать другим потокам. Для того, чтобы реализовать такую возможность в потоке c проверкой флага, надо использоваться Thread.yield. Хотя, судя по документации:

Yield is a heuristic attempt to improve relative progression between threads that would otherwise over-utilise a CPU. Its use should be combined with detailed profiling and benchmarking to ensure that it actually has the desired effect.

Это может не иметь никакого эффекта.