我正在使用 Swingworker 从 url 地址请求值以动态更改显示信息的版本。在某些情况下,该工人被取消。问题是我有时会得到 java.lang.InterruptedException (但不是每次我取消工作人员时)。我不知道如何处理它,而且我不知道它被扔到哪里,我无法调试它,因为我在短时间内进行大量版本更改时得到它(我使用滑块,当我拖动它时会发生这种情况一段时间)。一切正常,但我收到这个烦人的错误:
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at sun.plugin2.message.Queue.waitForMessage(Unknown Source)
at sun.plugin2.message.Pipe$2.run(Unknown Source)
at com.sun.deploy.util.Waiter$1.wait(Unknown Source)
at com.sun.deploy.util.Waiter.runAndWait(Unknown Source)
at sun.plugin2.message.Pipe.receive(Unknown Source)
at sun.plugin2.main.client.MessagePassingExecutionContext.doCookieOp(Unknown Source)
at sun.plugin2.main.client.MessagePassingExecutionContext.getCookie(Unknown Source)
at sun.plugin2.main.client.PluginCookieSelector.getCookieFromBrowser(Unknown Source)
at com.sun.deploy.net.cookie.DeployCookieSelector.getCookieInfo(Unknown Source)
at com.sun.deploy.net.cookie.DeployCookieSelector.get(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.setCookieHeader(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at org.dbwiki.web.applet.ValueRequester.doInBackground(ValueRequester.java:40)
at org.dbwiki.web.applet.ValueRequester.doInBackground(ValueRequester.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
同样在上面显示的每个异常之后,都会引发附加消息:
sun.plugin2.main.client.PluginMain: unrecognized message ID 46
有趣的是,只有当程序作为小程序在浏览器中运行时才会引发异常,如果程序从 api 作为小程序运行,则不会引发异常。
我的字符串工作者:
package org.dbwiki.web.applet;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.swing.SwingWorker;
public class ValueRequester extends SwingWorker<Void, Void> {
HtmlGenerator htmlGen;
ArrayList<String[]> versionsData;
String id;
private String dbName;
ValueRequester (HtmlGenerator htmlGen, ArrayList<String[]> versionData, int ver){
try {
this.htmlGen=htmlGen;
if (TreeVisualiser.typeParameter.equals(TreeVisualiser.StructureVisualiserParameter))
this.id=htmlGen.getElem().getVersionElementId(ver);
else if(TreeVisualiser.typeParameter.equals(TreeVisualiser.HistoryVisualiserParameter))
this.id=htmlGen.getElem().getId();
this.dbName=htmlGen.getElem().getDBName();
this.versionsData=versionData;
} catch (Exception e) {
e.printStackTrace();
}
}
protected Void doInBackground() throws Exception {
try{
String value="";
URL historyURL = new URL("http://127.0.0.1:8080/"+dbName+id+"?value");
URLConnection hc = historyURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(hc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
value+=line;
}
in.close();
this.htmlGen.formDisplayerHead();
this.htmlGen.formNodeDataHtml(value,this.versionsData);
this.htmlGen.formDisplayerTail();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
protected void done()
{
if (!this.isCancelled())
this.htmlGen.dataDisplayer.setText(this.htmlGen.getHtml());
}
}
我现在知道是什么原因造成的,如何处理它或至少如何隐藏它(因为一切正常)。任何帮助,将不胜感激。
更新:
我尝试在 ValueRequester.doInBackround() 中捕获此异常,但是我的 catch 语句没有捕获该异常。我更新的 doInBackground() 代码:
protected Void doInBackground() throws Exception {
try{
String value="";
URL historyURL = new URL("http://127.0.0.1:8080/"+dbName+id+"?value");
URLConnection hc = historyURL.openConnection();
InputStream inputStrm=hc.getInputStream();
InputStreamReader inputStrmRdr= new InputStreamReader(inputStrm);
this.in = new BufferedReader(inputStrmRdr);
String line;
while ((line = in.readLine()) != null) {
value+=line;
}
this.htmlGen.formDisplayerHead();
this.htmlGen.formNodeDataHtml(value,this.versionsData);
this.htmlGen.formDisplayerTail();
}catch (InterruptedException e){
System.out.println("Interrupted Exception caught!");
// e.printStackTrace();
}
return null;
}
不幸的是,仍然打印堆栈跟踪而不是我的系统输出消息。知道这里可能有什么问题吗?