使用swingworker的java.lang.InterruptedException [英] java.lang.InterruptedException using swingworker

查看:133
本文介绍了使用swingworker的java.lang.InterruptedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swingworker从URL地址请求值,以动态更改显示信息的版本.在某些情况下,此工作人员被取消.问题是我有时会收到java.lang.InterruptedException(但并非每次取消工作程序时都会收到).我不确定该怎么做,而且我不知道将它扔到哪里,我无法调试它,因为当我在短时间内进行大量版本更改时会得到它(我使用滑块,而拖动它时会发生这种情况一段时间).一切正常,但我收到这个烦人的错误:

I am using Swingworker to request value from url address to dynamically change a version of displayed information. At certain cases this worker is cancelled. The problem is that I get java.lang.InterruptedException sometimes (but not every time I cancel worker). I am not sure what to do with it, moreover I have no idea where it is thrown, I cannot debug it because I get it when I do lots of version changes in short time (I use slider and this happens when I am dragging it for some time) . Everything works fine but I get this annoying error:

 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)

同样,在上面显示的每个异常之后,还会引发其他消息:

Also after each exception which is showed above additional message is thrown:

sun.plugin2.main.client.PluginMain: unrecognized message ID 46

有趣的是,仅当程序在浏览器中作为小程序运行时才引发异常,如果程序从api作为小程序运行,则不会引发异常.

Interesting thing that exception is only being thrown when program is run in the browser as an applet, if program is run as applet from api no exceptions are thrown.

我的StringWorker:

My StringWorker:

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

}

}

我现在已经知道是什么原因导致它,如何处理它,或者至少如何隐藏它(因为一切正常).任何帮助将不胜感激.

I have now idea what causes it, how to handle it or at least how to hide it (as everything works normal). Any help would be appreciated.

更新:

我尝试在ValueRequester.doInBackround()中捕获此异常,但是我的catch语句未捕获该异常.我更新的doInBackground()代码:

I try to catch this exception in the ValueRequester.doInBackround(), however my catch statement don't catch the exception. My updated code of 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;
}

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

Unfortunately stack trace is still printed instead of my system out message. Any idea what could be wrong here?

推荐答案

据我所知,仅当其他一些线程在被阻塞的线程上调用Thread.interrupt()时,才会出现InterruptedException.在这种情况下,很明显当时被中断的线程处于wait()调用中.

As far as I know, an InterruptedException only occurs if some other thread calls Thread.interrupt() on a thread that is blocked. In this case, it is clear that the interrupted thread was in a wait() call at the time.

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

Looking at the SwingWorker code, it appears that the worker thread will get an interrupt if the thread that scheduled decides to call cancel(true) on it. Depending on what the worker thread is doing at the time, it may get an InterruptedException, or it may just have its Thread.interrupted flag set.

因此,解决您的问题的方法似乎是找出在SwingWorker实例上调用cancel(true)的内容.然后要么将其更改为不执行此操作……要么使您的工作人员类适当地处理InterruptedException.适当的行为可能是捕获异常并从call(...)

So the solution to your problem would appear to be to find out what is calling cancel(true) on the SwingWorker instance. Then either change it to not do that ... or make your worker class deal with the InterruptedException appropriately. The appropriate behaviour would probably be to catch the exception and quietly return from call(...)

这篇关于使用swingworker的java.lang.InterruptedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆