- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过 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/
我编写了一个 JUnit (4.10) 单元测试,它对 com.google.appengine.api.ThreadManager 进行以下调用: ThreadManager.currentRequ
App Engine 给出错误: com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call urlfe
我想通过 ThreadManager.createThreadForCurrentRequest 可运行方法中的 ChannelService.sendMessage 发送消息。但是当我在 doGet
我是一名优秀的程序员,十分优秀!