我正在使用此API来解析
中提供的维基百科转储http://code.google.com/p/wikixmlj/
我正在使用我的API
Class wiki
{
public void parseWiki()
{
PageCallbackHandler handler = new WikiPageCallbackHandler()
WikiXMLSAXParser.parseWikipediaDump(filepath, handler )
}
}
Class WikiPageCallbackHandler implements PageCallbackHandler
{
//Override
//This is the callback
void process(WikiPage page)
{
String Text = page.GetText();
//Write this text into a file
.....
}
}
问题出在所有回调完成之前我完成将其写入文件,应用程序正在终止,我没有得到所有回调。 我想知道为什么会发生这种情况,有没有办法保留我的主线程,以便我得到所有的回调。另外,我怎么知道我是否收到了所有回调?
由于
答案 0 :(得分:0)
您是否尝试在Oracle Docs使用'信号量'
以下是Semaphore
的使用示例:
class wiki
{
public void parseWiki(){
// initialize Semaphore object
Semaphore semaphore = new Semaphore(1);
//pass it to callback handler so it can release semaphore latter
PageCallbackHandler handler = new WikiPageCallbackHandler(semaphore);
//This is the async operation, right?
WikiXMLSAXParser.parseWikipediaDump(filepath, handler );
//wait until a permit is available (when semaphore.release() is called)
semaphore.acquire(); //this throw InterruptedException, please handle it else where
}
}
class WikiPageCallbackHandler implements PageCallbackHandler
{
private Semaphore semaphore;
public WikiPageCallbackHandler(Semaphore semaphore){
this.semaphore = semaphore;
}
//Override
//This is the callback
void process(WikiPage page)
{
try{
String Text = page.GetText();
//Write this text into a file
.....
}finally{
semaphore.release();
}
}
}