1.volatile,不推荐
    可以终止

    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. MyRunnable myRunnable = new MyRunnable();
    4. Thread thread = new Thread(myRunnable);
    5. thread.start();
    6. Thread.sleep(20);
    7. myRunnable.setFlag(true);
    8. }
    9. }
    10. class MyRunnable implements Runnable {
    11. private volatile boolean flag = false;
    12. public void setFlag(boolean flag) {
    13. this.flag = flag;
    14. }
    15. @Override
    16. public void run() {
    17. while (!flag) {
    18. System.out.println("----");
    19. }
    20. }
    21. }

    不可以终止

    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. MyRunnable myRunnable = new MyRunnable();
    4. Thread thread = new Thread(myRunnable);
    5. thread.start();
    6. Thread.sleep(20);
    7. myRunnable.setFlag(true);
    8. }
    9. }
    10. class MyRunnable implements Runnable {
    11. private volatile boolean flag = false;
    12. private BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(10);
    13. public void setFlag(boolean flag) {
    14. this.flag = flag;
    15. }
    16. @SneakyThrows
    17. @Override
    18. public void run() {
    19. while (!flag) {
    20. System.out.println("----");
    21. blockingQueue.put("1");
    22. }
    23. }
    24. }

    2.interrupt

    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. MyRunnable myRunnable = new MyRunnable();
    4. Thread thread = new Thread(myRunnable);
    5. thread.start();
    6. Thread.sleep(10);
    7. thread.interrupt();
    8. }
    9. }
    10. class MyRunnable implements Runnable {
    11. @Override
    12. public void run() {
    13. while (!Thread.currentThread().isInterrupted()) {
    14. System.out.println("----");
    15. }
    16. }
    17. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. MyRunnable myRunnable = new MyRunnable();
    4. Thread thread = new Thread(myRunnable);
    5. thread.start();
    6. Thread.sleep(100);
    7. thread.interrupt();
    8. }
    9. }
    10. class MyRunnable implements Runnable {
    11. @Override
    12. public void run() {
    13. try {
    14. while (true) {
    15. System.out.println("---");
    16. Thread.sleep(10);
    17. }
    18. } catch (InterruptedException e) {
    19. e.printStackTrace();
    20. }
    21. }
    22. }
    1. public class Test {
    2. public static void main(String[] args) throws InterruptedException {
    3. MyRunnable myRunnable = new MyRunnable();
    4. Thread thread = new Thread(myRunnable);
    5. thread.start();
    6. Thread.sleep(100);
    7. thread.interrupt();
    8. }
    9. }
    10. class MyRunnable implements Runnable {
    11. @Override
    12. public void run() {
    13. while (!Thread.currentThread().isInterrupted()) {
    14. try {
    15. System.out.println("---");
    16. // sleep响应中断后,会把interrupt状态清除
    17. Thread.sleep(10);
    18. } catch (InterruptedException e) {
    19. e.printStackTrace();
    20. Thread.currentThread().interrupt();
    21. }
    22. }
    23. }
    24. }