我想在新線程上分派任務,並且我希望此任務在其各個執行階段通知父線程。如下所示:在可以與父線程通信的新線程上分派任務
void unzip(Path source, Path destination, ObserverThread observer) {
int n = compute number of entries;
observer.notify("n: " + n);
while (there are more entries) {
observer.notify("Unzipping " + name of entry);
unzip the entry;
}
observer.notify("done");
}
什麼是這個用例的標準庫唯一的解決方案? (我想要外部依賴性。)
發佈臨時結果的鏈接很有用。謝謝! – missingfaktor
雖然這並不回答原來的問題。 SwingWorker的'publish'和'process'是'protected'。這個和它的架構迫使人們把所有的邏輯放在上述'SwingWorker'裏面。 – missingfaktor
邏輯進入你自己的類,但它確實需要擴展'SwingWorker'。另外,您可以將'addPropertyChangeListener'添加到SwingWorker,並且在進行進度時可以通知監聽器,或者您可以啓動自己的事件。 – Andrejs