Didn't get: How to implement mutex behavior in Java.
One minute response: You don't really need to, because each class instance provides it with synchronized blocks. Otherwise, you can implement a binary semaphore.
Didn't get: How do you implement semaphores in Java.
One minute response: Similarly to the way we did for C++, except you can use the instance's mutex to implement the blocking behavior. Probably the hardest part is deciding what kind of behavior you want, particularly with respect to interrupts.
Didn't get: How can we implement our own wait list to make it meet our needs.
One minute response: That depends on what your needs are. If you want fairness, you probably need to implement a queue of of lock objects with one thread per lock; if you want priority, make it a priority queue.
Didn't get: What is the best way to wake a specific thread.
One minute response: That depends on what you mean by best. Assuming the thread's blocked in a sleep()
or wait()
, set its interrupt flag. You need a reference to the thread to do that, and the code has to be able to handle the resulting interrupt. Alternatively, you could assign each thread its own lock object. With one thread per lock object, you can use notify()
unambiguously. The second solution is better than the first because it doesn't involve interrupts; the first solution is better than the second because because there's no race condition.
This page last modified on 16 July 2003.