2012-07-05 41 views
1

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

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 

有趣的事情是例外程序在运行时才抛出浏览器作为一个applet,如果程序从api作为applet运行,则不会引发异常。

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

} 

} 

我现在的想法是什么原因呢,该如何处理呢,或者至少如何隐藏它(因为一切正常)。任何帮助,将不胜感激。

UPDATE:

我试图抓住这个异常的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; 
} 

不幸的是堆栈跟踪还是印刷的,而不是我的系统出的消息。任何想法在这里可能是错的?

+0

悄悄返回这不是完整的堆栈跟踪,是吗?实际上你得到的是由此InterrupedException导致的IOException,对吧? – 2012-07-05 10:34:09

回答

2

据我所知,一个InterruptedException只发生,如果其他线程调用Thread.interrupt()被阻止的线程上。在这种情况下,显然中断的线程当时在拨打wait()

看着SwingWorker代码,看起来如果被调度的线程决定调用cancel(true),工作线程会得到一个中断。根据工作线程当时正在执行的操作,它可能会得到InterruptedException,或者它可能只设置了其Thread.interrupted标志。

因此,您的问题的解决方案似乎是要找出SwingWorker实例上调用cancel(true)的内容。然后要么改变它不这样做......或者让你的工人阶级适当地处理InterruptedException。适当的行为可能会发现异常并悄然从call(...)

+0

即使我删除了我的帖子在这里[我认为只有得到()返回异常](http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-threa)和不像你所描述的那样'只有在其他线程调用时才会出现',所有从SwingWorker调用的东西都没有直接返回异常 – mKorbel 2012-07-05 13:27:10

+0

@mKorbel - 我知道你在说什么,在某种意义上你是对的。但问题不在于抛出'get()'的异常。看看堆栈跟踪。 – 2012-07-05 14:02:04

+0

同意,每一个我看到,这只可能从创建一个新的对象作为局部变量,也许可能是错误的想法/错误在SwingWorker内创建这些对象(对象可以创建之前)或使用SwingWorker在所有类型的后台任务 – mKorbel 2012-07-05 15:30:25

2

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

就你的情况而言,根据你在“工作”中的距离有多远,任务可能会被中断。

有关Thread interruption here的详细信息,请参见。