简体   繁体   English

使用swingworker的java.lang.InterruptedException

[英]java.lang.InterruptedException using swingworker

I am using Swingworker to request value from url address to dynamically change a version of displayed information. 我正在使用Swingworker从url地址请求值来动态更改显示信息的版本。 At certain cases this worker is cancelled. 在某些情况下,该工人被取消。 The problem is that I get java.lang.InterruptedException sometimes (but not every time I cancel worker). 问题是我有时会得到java.lang.InterruptedException(但不是每次我取消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

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. 有趣的是,当程序在浏览器中作为applet运行时,只会抛出异常,如果程序从api作为applet运行,则不会抛出任何异常。

My StringWorker: 我的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. 任何帮助,将不胜感激。

UPDATE: 更新:

I try to catch this exception in the ValueRequester.doInBackround(), however my catch statement don't catch the exception. 我尝试在ValueRequester.doInBackround()中捕获此异常,但是我的catch语句没有捕获异常。 My updated code of doInBackground(): 我更新的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? 知道这里有什么不对吗?

To me it just looks like you are calling worker.cancel(true); 对我而言,看起来你正在调用worker.cancel(true); (assuming worker is your SwingWorker). (假设worker是你的SwingWorker)。 The true indicates that if the current Thread is in an interruptible state, the Thread can be interrupted. true表示如果当前Thread处于可中断状态,则可以中断Thread。 When this happens, it automatically throws an Exception indicating that the Thread has been interrupted allowing you to release resources and possibly do something. 发生这种情况时,它会自动抛出一个Exception,指示Thread已被中断,允许您释放资源并可能执行某些操作。 I guess in your case you can safely ignore this and simply close the opened streams. 我想在你的情况下,你可以放心地忽略这一点,只需关闭打开的流。

In your case, depending on how far you are in your "Work", the task may get interrupted or not. 在您的情况下,根据您在“工作”中的距离,任务可能会中断或不中断。

See more information on Thread interruption here . 此处查看有关线程中断的更多信息。

As far as I know, an InterruptedException only occurs if some other thread calls Thread.interrupt() on a thread that is blocked. 据我所知,只有当某个其他线程在被阻塞的线程上调用Thread.interrupt()时才会发生InterruptedException In this case, it is clear that the interrupted thread was in a wait() call at the time. 在这种情况下,很明显中断的线程当时处于wait()调用中。

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. 查看SwingWorker代码,如果调度的线程决定在其上调用cancel(true) ,则工作线程似乎将获得中断。 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. 根据工作线程当时的操作,它可能会出现InterruptedException ,或者它可能只设置了Thread.interrupted标志。

So the solution to your problem would appear to be to find out what is calling cancel(true) on the SwingWorker instance. 因此,您的问题的解决方案似乎是找出在SwingWorker实例上调用cancel(true)内容。 Then either change it to not do that ... or make your worker class deal with the InterruptedException appropriately. 然后将其更改为不执行此操作...或使您的工作类适当地处理InterruptedException The appropriate behaviour would probably be to catch the exception and quietly return from call(...) 适当的行为可能是捕获异常并静静地从call(...)返回call(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Process.waitFor()和java.lang.InterruptedException的Java多线程 - java multithreading using Process.waitFor() and java.lang.InterruptedException Azure MobileService EasyApi java.lang.interruptedException - Azure MobileService EasyApi java.lang.interruptedException 未处理的异常:java.lang.InterruptedException - Unhandled Exception: java.lang.InterruptedException 引起原因:java.lang.RuntimeException:java.lang.InterruptedException - Caused by: java.lang.RuntimeException: java.lang.InterruptedException SpringApplication.exit() retuns java.lang.InterruptedException: null - SpringApplication.exit() retuns java.lang.InterruptedException: null 执行 Apache HttpClient 时出现 java.lang.InterruptedException - java.lang.InterruptedException while executing Apache HttpClient java.lang.InterruptedException导致Web应用程序无法部署 - java.lang.InterruptedException causes web application not deploy 运行批处理文件时出现 java.lang.InterruptedException - java.lang.InterruptedException while running a batch file ReactiveException: java.lang.InterruptedException 当我用@HystrixCommand 标记方法时 - ReactiveException: java.lang.InterruptedException when I mark method with @HystrixCommand 执行Java类时发生异常。 java.lang.InterruptedException - An exception occured while executing the Java class. java.lang.InterruptedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM