ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How do you ensure that N thread can access N resources without deadlock?

user-image
تم إضافة السؤال من قبل Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
تاريخ النشر: 2014/05/24
ahmad mohammed
من قبل ahmad mohammed , JAVA team lead , edu-tek

A good way to implement such a mechanism is to use a queue, for example a thread safe BlockingQueueand have2 consumer threads:

  • the Printer Thread that takes jobs from the printerQueue
  • the Scanner Thread that takes jobs from the scannerQueue

Then your T0...T10 simply put jobs in one of the queues and wait (or not) for them to be executed when the Printer/Scanner is ready.

By doing that you remove the need to lock on the client/producer side and let the consumer side (Printer and Scanner) to manage their jobs.

مستخدم محذوف‎
من قبل مستخدم محذوف‎

You should not call wait() methods inside the resource. Should not call the other resources inside the resource.

المزيد من الأسئلة المماثلة