Java Reference
In-Depth Information
}
}
}
}. start ();
// In the main thread, wait a while then terminate the run.
try
try {
Thread . sleep ( 10 * 1000 );
} catch
catch ( InterruptedException ex ) {
// nothing to do
} finally
finally {
done = true
true ;
}
}
}
Because this is a simulation and the voting is random, it does not always come out 50/50. In
two consecutive runs, the following were the last line of each run:
Agree(6), Disagree(6)
Agree(9), Disagree(4)
See Also
The Lock interface also makes available Condition objects, which provide even more flex-
ibility. Consult the online documentation for more information.
Synchronizing Threads the Hard Way with wait( ) and
notifyAll( )
Problem
The synchronized keyword lets you lock out multiple threads but doesn't give you much
communication between them.
Solution
Use wait() and notifyAll() . Very carefully.
Search WWH ::




Custom Search