【发布时间】:2011-12-09 18:42:38
【问题描述】:
我有一个将我的 System.out 文本重定向到 Jtextarea 的应用程序。这很好用,但是当我在我的应用程序中调用其中一种方法时,会创建多个线程并使用闩锁计数器来等待它们完成。然后该方法调用latch.await(),以便在其他线程完成之前它不会完成其代码的运行。问题是,一旦调用了 latch.await() 代码,我的 JtextArea 就会停止发布文本,直到所有线程都完成。有什么想法吗? Eclipse 控制台能够在latch.await() 运行时继续发布,所以它必须是可能的。
示例: 从图形用户界面:
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("You pressed Start");
MyGoogleSearch startParsing = new MyGoogleSearch();
try {
startParsing.startParser(othoSelection); ...
我的谷歌搜索:
Enumeration e = hm.elements();
//Read in src/Ontology/Ontology.txt
//Put each line into the searchQuery ArrayQueue
while ((strLine = br.readLine()) != null)
{
searchQuery.put(strLine);
}
System.out.println("Finsihed loading");
//Create 32 threads (More threads allows you to pull data from Bing faster. Any more than 32 and Bing errors out (you start to pull data
//too fast)
for(int i = 0; i < 32; i++)
{
System.out.println("Starting thread: " + i);
new NewThread();
}
//Wait for all of the threads to finish
latch.await();
e = hm.keys();
//Write the URL's from the hashmap to a file
while (e.hasMoreElements())
{
out.write(e.nextElement() + "\n");
}
//close input/output stream
in.close();
out.close();
System.out.println("Done");
然后线程会做一些事情
MyGoogleSearch.latch.countDown();
【问题讨论】:
标签: java multithreading user-interface