是否可以将多个 Runnable 对象排队等待在一个线程中执行?不使用 ExecutorService 如何完成?如果这些 Runnable 对象之一抛出未经检查的异常会发生什么,它会停止执行线程吗?
最佳答案
Is this possible to queue multiple Runnable objects to wait for execution in one thread?
使用 ExecutorService。
How it can be done without using ExecutorService?
使用 BlockingQUeue 和线程。
What's gonna happen if one of those Runnable objects threw unchecked exception, does it stop executing thread?
这取决于你,因为你必须自己写这个。
对于 ExecutorService,抛出的 Throwable 会添加到 Future 对象中供您检查。很容易忘记执行此操作,在这种情况下,您需要向 Runnable 添加 try/catch block 。
关于java - 将 Runnable 对象排队以在一个线程中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12192154/