1

I'm doing an uni project about a computer system intended to be used in an hospital.

I have a TreeSet of Patient, where all the patients recovered in the hospital are stored. The class Patient has among other data some vital parameters associated to it. In addition to the main thread, I have three threads that generate the vital parameters of every Patient: I have a thread that generate blood pressure every 2 minutes, a thread that generate heartbeat every 5 minutes and a thread that generate temperature every 3 minutes. In the threads I have a BlockingQueue<Patient> patients, where every new Patient is added by the Producer and the threads have to perform the action described above for each Patient.

The problem is that I'm afraid I haven't quite understood the Producer-Consumer pattern.

My consumers are the three threads, but they are different types of classes, I have PressureChanger, TemperatureChanger, HeartBeatChanger. If I add patients the first thread gets the first patient from the Queue, the second thread the second one and the third thread the third one and then if I insert other patients no threads get them. How does it work with not only multiple Consumers, but also different classes who perform different actions, have different methods, as Consumers?

Jas
  • 21
  • 4
  • Use three queues surely? If you want to be fancy then LMax Disruptor suppliers multiple actions per queue item - probably rather overkill for a uni project, and also beyond your skill with concurrency; but might be fun to read about. – Boris the Spider Aug 31 '19 at 08:47
  • @BoristheSpider I thought about that, but isn't there a more efficient solution? Using three queues sounds a bit like an escamotage, but I'm not an expert and this is the first time I'm dealing with Java and concurrency. Oh just read about LMax Disruptor, yes it looks like it is way out of my league, but thank you for the suggestion! – Jas Aug 31 '19 at 08:48
  • Then use three queues. 1) get it working and the tests passing. 2) refactor for extensibility. 3) measure performance. 4) decide whether you need more performance and where. 3 queues is elegant in its simplicity. A simple alternative would be to completely rewrite to have **tasks** that have a `Patient` and have a `TaskProcessor` that executes all the tasks. Then put tasks in the queue. – Boris the Spider Aug 31 '19 at 08:49
  • So now you have a _pool_ of homogeneous workers and heterogeneous tasks rather than heterogeneous workers that have the tasks baked in. If that sound familiar, then it should - it's a `ExecutorService`. So you can just use one of those, have your 3 tasks `implements Runnable` and you're golden. As I say above though, perhaps get it working the simplest way first then refactor. – Boris the Spider Aug 31 '19 at 08:59
  • The three queues work, but now my problem is that all of my three threads have to sleep for n minutes before performing a task and in order to do it for every ```Patient``` I have used a for loop ```for(Patient patient: patients)``` but as I call the ```sleep()``` method in the for loop, every thread is blocked on the first patient. So maybe I should use multiple tasks, as you said, but for each thread? Sorry if I'm getting it all wrong. – Jas Aug 31 '19 at 09:24
  • `ScheduledExecutorService` – Boris the Spider Aug 31 '19 at 09:35

0 Answers0