On Wed, 14 Sep 2005 02:55:08 GMT, Vespasian <ve*@ves.net> wrote or
quoted :
notify() and wait() throw an exception if the calling thread does not
own the object's monitor. Does this imply that the check is made at
runtime, and if so, why can't it be made at compile time?
Threads don't exist at compile time. At compile time, you have
absolutely no idea how many threads there will be or which threads are
contending for access to which objects.
The very fact you asked this question indicates there is something
very basic you are misunderstandin g about threads. Unfortunately, I
don't know what your misconception is. I suggest reading up in a text
book on them, or at least reading my introduction to them at
http://mindprod.com/jgloss/thread.html
IF you have assembler background, I could tell you that each thread
has it own call stack and local variables stack, about a meg of
overhead. On a multicpu machine it can run simultaneously with other
threads. On a single cpu machine, there is timeslicing to give each
thread a short burt of CPU time, then it stops in mid flight and lets
another thread have a go. Each thread has its own set of hardware
registers including instruction pointer.
When threads use local variables there are no problems since they are
private to the thread. But when they use objects or instance or static
variables there is the potential of other threads accessing them at
the sime time. That is when you use synchronized to limit access to
one thread at a time to accessing an object.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.