我是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)
所以我的问题是:
当从一个/ doInBackground()调用它们时,上面这两个方法是否也在worker-thread中运行。应该是吧?
似乎是一个愚蠢的问题,但我有点不安,如果这个超时错误不是真的由一些线程混乱(而不是真正的http超时)产生..
感谢您的帮助!
答案 0 :(得分:1)
这两种方法在工作线程中运行,但是顺序执行。因此,analyzeHTML
在工作线程中首先被调用,然后在完成时,在同一个线程中调用download
。
答案 1 :(得分:0)
是的,doInBackground
正在与事件调度线程分开的一个线程中运行。您调用的任何方法都将在同一个线程上下文中调用,除非您以某种方式更改它。
所以,如果一切顺利,首先会调用analyzeHTML
,当它完成后,将调用download
。
在你的例子中,这也取决于第一种方法是否抛出相同类型的Exception
,第二种方法也不会被调用。