【发布时间】:2017-08-20 13:06:57
【问题描述】:
我正在使用一个扩展SwingWorker 的Worker 类,并在它的process 方法中处理String 值并将它们打印到JTextArea。
所以我做了很多 publish(some_string) 调用来将字符串打印到文本区域,但是当我执行我的工作线程时 - 它不会打印我在doInBackground() 方法。它错过了很多发布调用,我只得到了我想要的部分单词。
但是当我执行线程并放置一个断点并一步一步地跟随它我可以看到它确实打印了发布调用中的所有字符串。
做该做的事。
为什么它在调试模式下工作,而在正常模式下却不行?
我的代码:
public class Worker extends SwingWorker<Void , String>
{
public Worker(int int optionOfWork){};
protected Void doInBackground() throws Exception
{
...
publish("Hellow");
publish("This is a test");
...
// a lot of **publish(some_word) calls**
...
publish("Some more words");
}//doInBackground()
@override
protected void process(List<String> wordsToPrint)
{
String lastWordRecieved = wordsToPrint.get(wordsToPrint.size()-1);
mainWindowTextArea.append(lastWord); // not printing\appending all the words sent here by the publish calls
}
@override
protected void done()
{
publish("\nDone");
}
}//Worker class
我在主线程中所做的只是创建一个 Worker 对象并执行它:
Worker worker = new Worker();
我使用worker.excute();启动它
【问题讨论】:
-
请创建并发布一个有效的minimal reproducible example,我们可以编译和运行的代码向我们展示您的问题。
-
请查看编辑以回答。询问是否有任何令人困惑的地方。
标签: java multithreading swing jtextarea swingworker