Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

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

user-image
Question added by Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date Posted: 2014/05/24
ahmad mohammed
by 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

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.