I am trying to implement thread synchronization using monitors, for a simple queue problem, a waiting line of persons to use an ATM machine and prioritize old people.
问题:
回答1:
You could use one of provided implementations of a java.util.concurrent.BlockingQueue
interface instead of writing your own code e.g. java.util.concurrent.PriorityBlockingQueue
.
An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError). This class does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in ClassCastException).
Exploring classes in java.util.concurrent
package is a good exercise if you are learning about concurrency, this code is well written and documented.