1

我正在使用 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;
}

不幸的是,仍然打印堆栈跟踪而不是我的系统输出消息。知道这里可能有什么问题吗?

4

2 回答 2

3

据我所知,InterruptedException只有在其他线程调用Thread.interrupt()被阻塞的线程时才会发生。在这种情况下,很明显被中断的线程当时正在wait()调用中。

查看 SwingWorker 代码,如果调度的线程决定调用cancel(true)它,则似乎工作线程将获得中断。根据工作线程当时正在做什么,它可能会得到一个InterruptedException,或者它可能只是Thread.interrupted设置了它的标志。

因此,您的问题的解决方案似乎是找出调用实例cancel(true)的内容。SwingWorker然后要么将其更改为不这样做……要么让您的工人阶级InterruptedException适当地处理。适当的行为可能是捕获异常并从call(...)

于 2012-07-05T10:44:37.860 回答
2

对我来说,它看起来就像你在打电话worker.cancel(true);(假设工人是你的 SwingWorker)。true表示如果当前 Thread 处于可中断状态,则该 Thread 可以被中断。发生这种情况时,它会自动抛出一个异常,指示线程已被中断,从而允许您释放资源并可能做一些事情。我想在您的情况下,您可以放心地忽略这一点并简单地关闭打开的流。

就您而言,根据您在“工作”中的距离,任务可能会被中断或不被中断。

在此处查看有关线程中断的更多信息。

于 2012-07-05T10:35:11.787 回答