Finally, finally, figured out how to use java.lang.Object.wait()
and
java.lang.Object.notify()
. This crap is so nonintuitive to me. "Owner of
the object's monitor" indeed. Almost guaranteed I won't remember what I
learned today, I'll have to learn it all over again the next time I need it.
In case anyone else is having the same problem, here's the way I grok it:
Each thread can own the object's monitor for a short time by synchronizing on
that object. For example synchronized(stack) {doSomething();}
. One
thread's doSomething()
will call notify()
or
notifyAll()
, while the other's
will call wait()
or wait(millis)
.
I used it to show a splash screen within a
midlet for either 3 seconds or until a key is pressed or the stylus presses
against the screen. Of course, I could have schlocked something together by
pointerPressed()
setting a variable, and chopping up my
sleep()
into 50ms or
so, checking variables each loop, but I wanted to do this one correctly,
interrupt- or event-driven all the way. I'm tired of writing shitty code just
to get a product out the door.
last updated 2013-01-10 20:37:49. served from tektonic.jcomeau.com