线程基础机制

229 阅读1分钟
  • Daemon:线程守护。默认为false,可以在线程start前设置。
thread.setDaemon(true);
thread.start();

在所有非守护线程结束时,程序会杀死所有守护线程。

  • Sleep:Thread.sleep(millisec)会休眠当前执行线程。

休眠状态可以抛出InterruptException异常;

线程中需要对InterruptException进行处理,因为当前异常无法抛出到其他线程。

  • Yield:当前执行线程执行Thread.yield()方法代表线程礼让,可以切换其他线程执行,但是该方法只是对线程调度器进行建议,切换其他具有相同优先级的线程可以运行,但是实际哪个线程执行要看哪个线程抢占到时间片。

  • 线程中断

    • interrupt()

    调用一个线程的interrupt()方法,如果该线程当前处于阻塞或有限期等待或无限期等待状态时(不包括IO阻塞和等待锁的情况),会抛出InterruptException异常,该线程不会继续往下执行,直接结束线程。

    public class InterruptExample {
    
        private static class MyThread1 extends Thread {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("Thread run");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new MyThread1();
        thread1.start();
        thread1.interrupt();
        System.out.println("Main run");
    }
    
    // print
    Main run
    java.lang.InterruptedException: sleep interrupted
        at java.lang.Thread.sleep(Native Method)
        at InterruptExample.lambda$main$0(InterruptExample.java:5)
        at InterruptExample$$Lambda$1/713338599.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
    
    
    • interrupted()

    如果一个线程并未处于休眠或等待状态时,其他线程调用interrupt()方法并不会让该线程抛出InterruptException异常;此时该线程调用interrupted()方法会返回true,用来标记其他线程调用了该线程的中断方法,可以根据标记决定是否结束线程执行。

    一般适用于固定时长的无限循环中。

    public class InterruptExample {
    
        private static class MyThread2 extends Thread {
            @Override
            public void run() {
                while (!interrupted()) {
                    // ..
                }
                System.out.println("Thread end");
            }
        }
    }
    
    
    public static void main(String[] args) throws     InterruptedException {
        Thread thread2 = new MyThread2();
        thread2.start();
        thread2.interrupt();
    }
    
    // print
    Thread end
    
    • Executor的中断方法

    直接调用executor的shutdown()方法会等待所有线程都执行完毕后关闭线程池;

    调用showdownNow()方法相当于在给每个线程执行interrupt()方法;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("Thread run");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.shutdownNow();
        System.out.println("Main run");
    }
    
    // print
    // 并未执行Thread run,因为执行shutdownNow()方法相当于给该线程执行了interrupt()方法
    Main run
    java.lang.InterruptedException: sleep interrupted
        at java.lang.Thread.sleep(Native Method)
        at ExecutorInterruptExample.lambda$main$0(ExecutorInterruptExample.java:9)
        at ExecutorInterruptExample$$Lambda$1/1160460865.run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
    

    如果想中断线程池中某个具体线程,可以采用submit方法进行线程执行,返回Future<?>类,调用该类的cancel(true)方法来中断该线程。

    Future<?> future = executorService.submit(() -> {
        // ..
    });
    future.cancel(true);
    
  • 线程状态:

    • 新建
    • 就绪
    • 运行
    • 阻塞(等待获取到锁)
    • 无限期等待:没有设置时间参数的wait, join等。
    • 有限期等待:设置了timeout参数的wait, join等。