0

我是 Thread 的新手,想使用 SwingWorker 来更新我的 GUI,而另一个线程正在分析一些 html 并下载文件..

这是我的代码:

protected Void doInBackground() throws Exception {

    analyzeHTML();
    download();

    return null;
}

整个通话有时会失败:

   SEVERE: null
java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:150)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:579)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1322)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:429)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:446)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:446)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:410)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:164)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:153)
    at hanserdownloader.LinkProcessorThread.analyzeHTML(LinkProcessorThread.java:208)
    at hanserdownloader.LinkProcessorThread.doInBackground(LinkProcessorThread.java:139)
    at hanserdownloader.LinkProcessorThread.doInBackground(LinkProcessorThread.java:113)
    at javax.swing.SwingWorker$1.call(SwingWorker.java:296)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at javax.swing.SwingWorker.run(SwingWorker.java:335)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

所以我的问题是:

当从 one/doInBackground() 调用这两种方法时,它们是否也在工作线程中运行。应该是吧?

似乎是一个愚蠢的问题,但我有点不安,如果这个超时错误不是真的由一些线程混乱(而不是真正的http超时)产生的......

谢谢您的帮助!

4

2 回答 2

1

这两种方法都在工作线程中运行,但是是按顺序运行的。因此,analyzeHTML首先在工作线程中调用,然后在完成后download在同一个线程中调用。

于 2013-09-06T00:59:28.047 回答
0

是的,doInBackground在与事件调度线程不同的线程中运行。您调用的任何方法都将在同一线程上下文中调用,除非您以某种方式对其进行更改。

所以,如果一切顺利,首先analyzeHTML会被调用,当它完成时,download会被调用。

但是,在您的示例中,这也取决于如果第一个方法抛出Exception相同类型的,则不会调用第二个方法。

于 2013-09-06T01:01:15.780 回答