Java 线程与线程池类/接口继承谱系图
1. 线程相关类与接口关系
implements
returns
«interface»
Runnable
+run()
«interface»
Callable<T>
+call()
«interface»
Future<T>
+get()
+cancel()
+isDone()
Thread
-Runnable target
+start()
+run()
+join()
+interrupt()
+sleep(long)
«interface»
Executor
+execute(Runnable)
«interface»
ExecutorService
+submit(Callable<T>)
+shutdown()
+shutdownNow()
AbstractExecutorService
+submit(Runnable)
ThreadPoolExecutor
-corePoolSize: int
-maximumPoolSize: int
+execute(Runnable)
«interface»
ScheduledExecutorService
+schedule(Runnable, long, TimeUnit)
2. 线程常用方法及作用
Thread 类核心方法
方法 作用 示例 start()
启动新线程,JVM 调用 run()
方法。 new Thread(task).start();
join()
阻塞当前线程,直到目标线程执行完毕。 thread.join();
sleep(long ms)
让当前线程休眠指定毫秒数,不释放锁 。 Thread.sleep(1000);
interrupt()
中断线程(设置中断标志位)。若线程在阻塞中(如 sleep
),抛出 InterruptedException
。 thread.interrupt();
isAlive()
判断线程是否处于活动状态(已启动但未终止)。 if (thread.isAlive()) { ... }
3. 线程池常用方法及作用
ExecutorService 接口核心方法
方法 作用 示例 execute(Runnable task)
提交无返回值的任务到线程池。 executor.execute(() -> System.out.println("Task"));
submit(Callable task)
提交有返回值的任务,返回 Future
对象。 Future<String> future = executor.submit(() -> "Result");
shutdown()
平滑关闭线程池:不再接受新任务,但会执行完已提交的任务。 executor.shutdown();
shutdownNow()
立即关闭线程池:尝试中断所有正在执行的任务,并返回未执行的任务列表。 List<Runnable> tasks = executor.shutdownNow();
awaitTermination()
阻塞直到所有任务完成,或超时发生,或线程被中断。 executor.awaitTermination(10, TimeUnit.SECONDS);
4. 方法使用场景对比
join()
vs shutdown()
方法 作用对象 使用场景 示例 join()
Thread
对象等待某个线程执行完毕。 主线程等待子线程完成后再继续。 shutdown()
ExecutorService
关闭线程池,不再接受新任务,等待已提交任务完成。 应用退出时优雅关闭线程池。
execute()
vs submit()
vs start()
方法 所属类/接口 返回值 支持任务类型 异常处理 start()
Thread
无 Runnable
需在 run()
内处理异常。 execute()
Executor
无 Runnable
线程池会吞掉未捕获异常。 submit()
ExecutorService
Future
Runnable/Callable
通过 Future.get()
捕获异常。
5. 核心代码示例
线程的 join()
方法
Thread thread1 = new Thread ( ( ) -> System . out. println ( "Thread 1" ) ) ;
Thread thread2 = new Thread ( ( ) -> System . out. println ( "Thread 2" ) ) ; thread1. start ( ) ;
thread2. start ( ) ;
thread1. join ( ) ;
thread2. join ( ) ;
System . out. println ( "All threads completed" ) ;
线程池的 shutdown()
ExecutorService executor = Executors . newFixedThreadPool ( 2 ) ;
executor. execute ( ( ) -> System . out. println ( "Task 1" ) ) ;
executor. execute ( ( ) -> System . out. println ( "Task 2" ) ) ; executor. shutdown ( ) ;
executor. awaitTermination ( 10 , TimeUnit . SECONDS ) ;
通过 submit()
获取任务结果
ExecutorService executor = Executors . newSingleThreadExecutor ( ) ;
Future < Integer > future = executor. submit ( ( ) -> { TimeUnit . SECONDS . sleep ( 1 ) ; return 42 ;
} ) ; Integer result = future. get ( ) ;
System . out. println ( "Result: " + result) ;
executor. shutdown ( ) ;
6. 注意事项
join()
的陷阱 :
若主线程调用 join()
后,目标线程长时间不结束,会导致主线程永久阻塞。 可通过设置超时避免:thread.join(5000);
(最多等待5秒)。 shutdown()
的正确使用 :
调用 shutdown()
后仍提交任务会触发 RejectedExecutionException
。 结合 awaitTermination()
确保所有任务完成。 线程池资源释放 :
必须显式关闭线程池(shutdown()
或 shutdownNow()
),否则 JVM 不会退出。
总结
线程类与接口 :Runnable
、Callable
、Thread
、Executor
框架构成 Java 并发基础。核心方法 : join()
:用于线程间同步。shutdown()
:优雅关闭线程池。submit()
:提交有返回值的任务。 方法选择 : 直接操作线程用 start()
和 join()
。 线程池任务管理用 execute()
和 submit()
。