Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date de publication: 2014/05/24
ahmad mohammed
par 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.

More Questions Like This

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?