java - How to know when two threads are finished in Swing -
i have perform 2 tasks. 2 threads perform each task simultaneously. tasks don't share data.
before tasks start, shown dialog info "wait, processing...".
here codes:
final jdialog dialog = new jdialog(this, true); swingworker<void, void> worker = new swingworker<void, void>() { @override protected void doinbackground() throws exception { // job homecoming null; } @override protected void done() { // must close dialog? other finished? } }; swingworker<void, void> worker2 = new swingworker<void, void>() { @override protected void doinbackground() throws exception { // job homecoming null; } @override protected void done() { // must close dialog? other finished? } }; worker.execute(); worker2.execute(); dialog.setvisible(true); // must close dialog?
i close dialog when 2 threads ended. how know when ended? , when should close dialog?
update: threads must run simultaneously, not in sequential mode.
create countdownlatch
, set 2
create 2 swingworkers
, passing each reference countdownlatch
. in there done
methods, phone call countdown
on latch. in done
method, called regardless of how doinbackground
method exited (ie in case throws exception
) create 3rd swingworker
, passing reference countdownlatch
, in worker wait latch in doinbackground
method. 1 time swingworker
's done method called, should able dispose of dialog safely
java multithreading swing
No comments:
Post a Comment