gpt4 book ai didi

java - 当我在 Google App Engine Javaservlet 中使用 ThreadManager 和 ChannelService 时,出现 "Interrupted while waiting on semaphore"

转载 作者:行者123 更新时间:2023-11-30 03:07:33 26 4
gpt4 key购买 nike

我想通过 ThreadManager.createThreadForCurrentRequest 可运行方法中的 ChannelService.sendMessage 发送消息。但是当我在 doGet 中调用此方法时,jetty 记录器中出现错误消息。这是我在 HttpServlet 类中的代码:

    @Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
name = req.getParameter("name");
ThreadManager.createThreadForCurrentRequest(new Runnable() {
@Override
public void run() {
for (int i = 10; i < 100; i++) {
d = com.google.appengine.repackaged.org.joda.time.DateTime.now();
serverTime = String.valueOf(d.getMillis());
ChannelMessage channelMessage = new ChannelMessage("logger", i+ " Message..... " + name + " Server Time: " + serverTime + " Client Time:");
getChannelService().sendMessage(channelMessage);

}

}
}).start();

}

这是 jetty 记录器中的错误:

Exception in thread "Thread-12" java.lang.RuntimeException: Interrupted while waiting on semaphore:
at com.google.appengine.tools.development.ApiProxyLocalImpl.makeAsyncCall(ApiProxyLocalImpl.java:193)
at com.google.appengine.tools.development.ApiProxyLocalImpl.makeSyncCall(ApiProxyLocalImpl.java:156)
at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:118)
at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:67)
at com.google.appengine.api.channel.ChannelServiceImpl.sendMessage(ChannelServiceImpl.java:85)
at com.roundtableapps.pluto.backend.MyServlet$1.run(MyServlet.java:42)
at com.google.appengine.tools.development.RequestThreadFactory$1$1$2.run(RequestThreadFactory.java:110)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.tools.development.RequestThreadFactory$1$1.run(RequestThreadFactory.java:107)
Caused by: java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1301)
at java.util.concurrent.Semaphore.acquire(Semaphore.java:317)
at com.google.appengine.tools.development.ApiProxyLocalImpl.makeAsyncCall(ApiProxyLocalImpl.java:190)
... 8 more

如何解决这个问题?

最佳答案

ThreadManager.createThreadForCurrentRequest 线程将在请求后立即停止(中断)。在退出 doGet 方法之前,您必须在那里等待,直到完成。

所以基本上这样的线程没有任何意义。另外,您在同一个线程中完成所有艰苦的工作(我的意思是循环 block ),而不是并行化它。

类似这样的事情:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
name = req.getParameter("name");
List<Future> waits = new ArrayList<>(90);
ExecutorService executors = Executors.newFixedThreadPool(50, ThreadManager.currentRequestThreadFactory());
for (int i = 10; i < 100; i++) {
waits.add(executors.submit(new Callable<Boolean>() {
Boolean call() {
d = com.google.appengine.repackaged.org.joda.time.DateTime.now();
serverTime = String.valueOf(d.getMillis());
ChannelMessage channelMessage = new ChannelMessage("logger", i+ " Message..... " + name + " Server Time: " + serverTime + " Client Time:");
getChannelService().sendMessage(channelMessage);
return true;
}

}))
}
for (Future f: waits) {
f.get()
}

}

如果您只需要一个线程,那么与线程相关的所有部分都可以删除:

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
name = req.getParameter("name");
for (int i = 10; i < 100; i++) {
d = com.google.appengine.repackaged.org.joda.time.DateTime.now();
serverTime = String.valueOf(d.getMillis());
ChannelMessage channelMessage = new ChannelMessage("logger", i+ " Message..... " + name + " Server Time: " + serverTime + " Client Time:");
getChannelService().sendMessage(channelMessage);
}
}

关于java - 当我在 Google App Engine Javaservlet 中使用 ThreadManager 和 ChannelService 时,出现 "Interrupted while waiting on semaphore",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370032/

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