Hi all,
In the book two different approaches to evade busy waiting are shown:
Code:
Object theLock = new Object();
synchronized( theLock ){
Thread task = new TheTask( theLock );
task.start();
try {
theLock.wait();
}
catch( InterruptedException e ){
.... // do something if interrupted
}
}
and
Code:
Thread task = new TheTask();
synchronized( task ){
task.start();
try {
task.wait();
}
catch( InterruptedException e ){
.... // do something if interrupted
}
}
Are these two completely interchangeable or are there instances when one should be used over the other?