How to implement a simple thread synchronization using monitors for a waiting line

问题: 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. 回答...

问题:

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.

  • 发表于 2019-01-05 22:50
  • 阅读 ( 202 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除