生命周期

说说线程的生命周期和状态?

创建方式

  • extends Thread

  • implements Runnable

  • implements Callable

    Callable<String> callable = () -> {
        return "hello world";
    };
     
    FutureTask<String> futureTask = new FutureTask<>(callable);
    Thread thread = new Thread(futureTask);
    thread.start();
  • 线程池

run vs start

  • start 启动线程
  • run 直接执行

Thread#sleep vs Object#wait

https://javaguide.cn/java/concurrent/java-concurrent-questions-01.html#thread-sleep-方法和-object-wait-方法对比

Thread.join()

public static void main(String[] args) throws InterruptedException {
    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 1 running");
        }
    });
 
    thread1.start();
	// main 主线程等待 thread1 执行完成
	thread1.join();
 
    System.out.println("Main 1 running");
}

Thread.sleep(0)的作用

让线程主动释放CPU时间片, 让其他线程可以进行一次公平的争抢

Thread.isAlive()

  • 判断线程是否存活
  • 如果被锁, 比如 synchronized (t), t.isAlive() 返回 true

线程同步的方式

See Also