使用swingworker的java.lang.InterruptedException

使用swingworker的java.lang.InterruptedException,java,swing,interrupted-exception,Java,Swing,Interrupted Exception,我正在使用Swingworker从url地址请求值,以动态更改显示信息的版本。在某些情况下,此工作人员被取消。问题是我有时会得到java.lang.InterruptedException(但不是每次取消worker时)。我不知道该怎么处理它,而且我也不知道它被扔到了哪里,我无法调试它,因为我在短时间内做了大量版本更改时得到了它(我使用slider,当我拖动它一段时间时会发生这种情况)。一切正常,但我有一个恼人的错误: java.lang.InterruptedException at jav

我正在使用Swingworker从url地址请求值,以动态更改显示信息的版本。在某些情况下,此工作人员被取消。问题是我有时会得到java.lang.InterruptedException(但不是每次取消worker时)。我不知道该怎么处理它,而且我也不知道它被扔到了哪里,我无法调试它,因为我在短时间内做了大量版本更改时得到了它(我使用slider,当我拖动它一段时间时会发生这种情况)。一切正常,但我有一个恼人的错误:

 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());

}

}

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

对我来说,你好像在呼叫
工作者。取消(true)(假设工人是您的SwingWorker)。
true
表示如果当前线程处于可中断状态,则可以中断线程。发生这种情况时,它会自动抛出一个异常,指示线程已被中断,从而允许您释放资源并可能执行某些操作。我想在你的情况下,你可以安全地忽略这一点,只需关闭打开的流

在你的情况下,取决于你的“工作”有多远,任务可能会被打断或不被打断


查看有关的详细信息。

对我来说,您似乎正在呼叫
工作者。取消(true)(假设工人是您的SwingWorker)。
true
表示如果当前线程处于可中断状态,则可以中断线程。发生这种情况时,它会自动抛出一个异常,指示线程已被中断,从而允许您释放资源并可能执行某些操作。我想在你的情况下,你可以安全地忽略这一点,只需关闭打开的流

在你的情况下,取决于你的“工作”有多远,任务可能会被打断或不被打断


请参阅有关的详细信息。

据我所知,只有在被阻塞的线程上有其他线程调用
thread.interrupt()
时才会发生
中断异常。在本例中,很明显,中断的线程当时处于
wait()
调用中

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


因此,问题的解决方案似乎是找出在
SwingWorker
实例上调用
cancel(true)
的是什么。然后要么把它改成不这样做。。。或者让您的工人类适当地处理
InterruptedException
。适当的行为可能是捕获异常并从
调用(…)

安静地返回。据我所知,只有在被阻塞的线程上有其他线程调用
thread.interrupt()
时才会发生
中断异常。在本例中,很明显,中断的线程当时处于
wait()
调用中

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


因此,问题的解决方案似乎是找出在
SwingWorker
实例上调用
cancel(true)
的是什么。然后要么把它改成不这样做。。。或者让您的工人类适当地处理
InterruptedException
。适当的行为可能是捕获异常并从
调用(…)

安静地返回,这不是完整的堆栈跟踪,是吗?实际上,你得到的是一个由InterrupedException引起的IOException,对吗?这不是完整的堆栈跟踪,是吗?事实上,你得到的是一个由InterrupedException引起的IOException,对吗?即使我在这里删除了我的帖子,也不像你所描述的那样,只有在一些其他线程调用时才会发生
,这是一个常见的问题,从SwingWorker调用的所有东西都不会返回异常directly@mKorbel-我知道你在说什么,从某种意义上说,你是对的。但问题不在于
get()
抛出的异常。查看stacktrace.agreed,aaach我看到,这只可能通过创建一个新的对象作为局部变量来实现,可能是在SwingWorker内部创建这些对象(对象可以在之前创建)或使用SwingWorker来完成此类后台任务的想法/错误。例如,有时使用cancel(true)来取消实例方法在某些情况下,这是必要的。然而,我不确定在哪里可以捕捉到这个异常。我尝试包围cancel()、execute()和doInBackground()函数并捕获此异常,但没有任何效果。我绝对是线程编程的初学者,所以如果我的一些逻辑没有太多意义或者我错过了一些非常明显的东西,请原谅。你必须在
ValueRequester.doInBackground
中或者在匿名类的
call
方法中捕捉它,你使用它来调用doInBackground。甚至我在这里删除了我的帖子,而不是像你一样仅当一些其他线程调用时才会出现描述的
,存在一个常见问题,即从SwingWorker调用的所有内容都不会返回异常directly@mKorbel-我知道你在说什么,从某种意义上说,你是个骗子
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;
}