gpt4 book ai didi

android - 关闭 SpeechClient 时出现 java.util.concurrent.RejectedExecutionException

转载 作者:行者123 更新时间:2023-11-29 23:03:27 25 4
gpt4 key购买 nike

我正在使用 Google Speech Client 将语音转换为文本,它工作正常,但当我尝试将其关闭时抛出异常。

这是我用来初始化的代码。

private val mSpeechClient by lazy {

applicationContext.resources.openRawResource(R.raw.credentials).use {
SpeechClient.create(SpeechSettings.newBuilder()
.setCredentialsProvider { GoogleCredentials.fromStream(it) }
.build())
}
}

但是当我调用

mSpeechClient.shutDown()

它抛出这个异常。

 Process: com.google.cloud.examples.speechrecognition, PID: 10080
java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@11065d8d rejected from java.util.concurrent.ScheduledThreadPoolExecutor@3cbffe24[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 2]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:298)
at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:503)
at java.util.concurrent.ScheduledThreadPoolExecutor.execute(ScheduledThreadPoolExecutor.java:592)
at io.grpc.internal.SerializingExecutor.schedule(SerializingExecutor.java:93)
at io.grpc.internal.SerializingExecutor.execute(SerializingExecutor.java:86)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl.closed(ClientCallImpl.java:594)
at io.grpc.internal.DelayedStream$DelayedStreamListener$4.run(DelayedStream.java:418)
at io.grpc.internal.DelayedStream$DelayedStreamListener.delayOrExecute(DelayedStream.java:372)
at io.grpc.internal.DelayedStream$DelayedStreamListener.closed(DelayedStream.java:415)
at io.grpc.internal.AbstractClientStream$TransportState.closeListener(AbstractClientStream.java:392)
at io.grpc.internal.AbstractClientStream$TransportState.access$300(AbstractClientStream.java:200)
at io.grpc.internal.AbstractClientStream$TransportState$1.run(AbstractClientStream.java:376)
at io.grpc.internal.AbstractClientStream$TransportState.deframerClosed(AbstractClientStream.java:245)
at io.grpc.internal.Http2ClientStreamTransportState.deframerClosed(Http2ClientStreamTransportState.java:31)
at io.grpc.okhttp.OkHttpClientStream$TransportState.deframerClosed(OkHttpClientStream.java:275)
at io.grpc.internal.MessageDeframer.close(MessageDeframer.java:233)
at io.grpc.internal.MessageDeframer.closeWhenComplete(MessageDeframer.java:195)
at io.grpc.internal.AbstractStream$TransportState.closeDeframer(AbstractStream.java:180)
at io.grpc.internal.AbstractClientStream$TransportState.transportReportStatus(AbstractClientStream.java:379)
at io.grpc.okhttp.OkHttpClientTransport.startGoAway(OkHttpClientTransport.java:739)
at io.grpc.okhttp.OkHttpClientTransport.access$1900(OkHttpClientTransport.java:92)
at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.run(OkHttpClientTransport.java:936)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)

最佳答案

RejectedExecutionException 被抛出,因为新任务显然是在 ThreadExecutor 关闭后提交的。发生这种情况是由于 ThreadExecutor 中设置的默认处理程序策略 - ThreadPoolExecutor.AbortPolicy

  • In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.

由于您正在手动关闭您的客户端 mSpeechClient.shutDown() 及其内部的 ThreadExecutor,为了解决您可能需要更改处理程序策略的问题到以下之一:

  • In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
  • In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
  • In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

以上,是对引用自ThreadPoolExecutor的handler policies描述的解释Java文档。

为此,您需要使用您选择的策略设置创建 ThreadExecutor 的自定义实例,然后将其传递给您的客户端。这应该可以解决问题。

关于android - 关闭 SpeechClient 时出现 java.util.concurrent.RejectedExecutionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56754174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com