考慮下面的代碼示例,其中我刪除了所有的錯誤,以保持它的可讀性處理代碼:如何延遲方法返回,直到文件完全讀
public class MyClass {
protected Something myAttribute;
protected boolean busy = true;
public ReactiveLogger() {
new Thread(new FileListener()).start();
}
private class FileListener implements Runnable {
@Override
public void run() {
RandomAccessFile raf = new RandomAccessFile(LOG_FILE, "rw");
while (true) {
final String line = cutFirstLine(raf);
if (line == null) {
busy = false;
Thread.sleep(1000);
} else {
busy = true;
// handeLogMessage changes myAttribute
handleLogMessage(line);
}
}
}
// definition of cutFirstLine etc.
}
// definition of handleLogMessage etc.
public Something getSomething() {
while (busy) {
Thread.sleep(100);
}
return myAttribute;
}
}
所以我MyClass
類讀取的日誌文件背景(在另一個線程中),並使用從日誌文件中讀取的每一行更新屬性myAttribute
。只要日誌文件中有條目並且我的屬性已更新,則應該延遲getter函數getMyAttribute()
的返回值。只要日誌文件中沒有更多條目,getMyAttribute()
應返回myAttribute
。
儘管此代碼示例按需要工作,但它似乎並不是最優雅的解決方案。目前尚不清楚線程應該睡多久以獲得最佳結果。如果只是在getter函數中刪除對Thread.sleep
的調用,則程序將凍結。但是,如果我將睡眠方法的值設置得太高,則執行時間也會太長。
我怎樣才能以更好的方式實現相同?我已經看過Java多線程/併發編程資源,但沒有任何東西(如Java的)似乎適合這種情況。
看看'Thread.join()' –
根據你描述的目標,我不確定我明白爲什麼需要兩個線程。如果你的其他線程阻塞直到找到所需的結果,那麼不要打擾兩個線程。 –
'Thread.join()'不能幫助afaik,因爲沒有線程可以結束。 'getSomething()'可以被多次調用。 'run()'方法永遠執行,因爲在任何時候,都可以將某些東西再次寫入日誌文件。 – Simon