ThreadPoolExecutor.execute发生异常时最后为啥要添加空worker对象
时间: 2023-11-23 08:29:10 浏览: 36
在 ThreadPoolExecutor 中,如果在执行任务时发生异常,那么会调用 `workerDone()` 方法,将执行任务的 worker 数量减一。如果此时 worker 数量为 0,则线程池中没有可用的线程了,因此无法继续执行任务。为了避免这种情况,ThreadPoolExecutor 在发生异常时会创建一个空的 Worker 对象并添加到 worker 队列中,这个空的 Worker 并不会执行任何任务,但它会占用一个线程,以确保线程池中至少有一个可用的线程,从而保证线程池的可用性。
相关问题
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: EntityManagerFactory is closed at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:130) at org.flowable.common.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:56) at org.flowable.common.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30) at org.flowable.common.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:56) at org.flowable.common.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:51) at org.flowable.common.engine.impl.query.AbstractQuery.list(AbstractQuery.java:117) at org.flowable.eventregistry.impl.management.DefaultEventRegistryChangeDetectionManager.detectChanges(DefaultEventRegistryChangeDetectionManager.java:50) at org.flowable.eventregistry.impl.management.EventRegistryChangeDetectionRunnable.run(EventRegistryChangeDetectionRunnable.java:35) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
这个异常是由于在执行事务期间无法打开JPA EntityManager引起的。其中嵌套的异常是“EntityManagerFactory is closed”。这可能是由于以下原因之一:
1. 在entityManagerFactory关闭之后,尝试使用它。
2. entityManagerFactory尚未成功初始化。
3. 数据库连接池中没有足够的连接可供使用。
您可以尝试重新启动应用程序或检查数据库连接是否正确。您还可以尝试增加数据库连接池的大小,以确保有足够的连接可用。如果这些步骤无法解决问题,请提供更多上下文和详细信息,以便我可以提供更准确的答案。
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.bawei.xuhe, PID: 22989 java.net.UnknownServiceException: CLEARTEXT communication to 10.59.9.18 not permitted by network security policy at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:188) at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106) at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.kt:221) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154) at retrofit2.OkHttpCall.execute(OkHttpCall.java:204) at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41) at io.reactivex.Observable.subscribe(Observable.java:10179) at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34) at io.reactivex.Observable.subscribe(Observable.java:10179) at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39) at io.reactivex.Scheduler$1.run(Scheduler.java:134) at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59) at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) I/Process: Sending signal. PID: 22989 SIG: 9
这一个 Android 运行时错误,指示您的应用程序尝试使用不安全的明文通信与 IP 地址为 10.59.9.18 的服务器进行交互,但是 Android 的网络安全策略禁止了此类操作。
为了解决此问题,您可以尝试以下几种方法:
1. 使用 HTTPS 协议替代明文通信。
2. 在 AndroidManifest.xml 文件中添加以下代码,以允许您的应用程序使用不安全的明文通信:
```
<application
...
android:usesCleartextTraffic="true">
...
</application>
```
3. 在您的网络请求中使用 Android 网络安全配置,以允许特定的 IP 地址使用明文通信,例如:
```
<network-security-config>
<base-config cleartextTrafficPermitted="false">
<trust-anchors>
<certificates src="@raw/my_ca"/>
</trust-anchors>
</base-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.59.9.18</domain>
</domain-config>
</network-security-config>
```
请注意,这些解决方案中的某些选项可能会影响您应用程序的安全性,请谨慎选择。