我在Google App Engine中的Runnable对象上创建了一个后台线程,如下所示:
BackendService s = new BackendService();
thread = ThreadManager.createBackgroundThread(s);
thread.start();
然而 - 在初始化时获取PersistenceManagerFactory,我喜欢这样:
private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactional");
我得到了一个类加载器异常:
Uncaught exception from servlet
com.google.apphosting.runtime.FatalError: A not-user-defined ClassLoader was set as the thread'scontextClassLoader: sun.misc.Launcher$AppClassLoader@1a8c4e7
at com.google.appengine.runtime.Request.process-f71d5e950ca508ff(Request.java)
at java.security.AccessController.doPrivileged(AccessController.java:34)
我做错了什么?
答案 0 :(得分:5)
我有同样的问题(使用JPA。)似乎有效的解决方案是手动设置Runnable中的类加载器。
在调用对象:
private static Thread thread;
private static ClassLoader cl;
在调用方法时:
...
cl = getClass().getClassLoader();
thread = ThreadManager.createBackgroundThread(...);
thread.start();
...
在Runnable run()中:
thread.setContextClassLoader(cl);
...
答案 1 :(得分:0)
`Thread thread = ThreadManager.createBackgroundThread(new Runnable() { public void run() { ... ClassLoader cl = getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(cl); ... } });`