Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How java avoid deadlock ? 1

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
How java avoid deadlock ?
 
Perhaps you would care to elaborate a little ?

--------------------------------------------------
Free Database Connection Pooling Software
 
hi,
suppose A and B are two processes . A depends upon B and also B depends upon A to satisfy a condition. Then a deadlock will occur.right ? now ,how do i program in java so that it can avoid deadlock ?


i know abt synchronisation (or object locking ) but that is for data consistency and not a solution for deadlock. right ?

so i want to know how to avoid deadlock in java ? which way i should program to avoid deadlock ?

N.B please let me know if you are not clear abt my question.

thanks
 
Well, IHMO there are two parts of synchronization:

- The mechanisms you use to avoid it, i.e. semaphores, monitors ... In Java, that's being done by synchronizing methods mainly for the moment. I think this will be improved for 1.5 with Semaphores and so on. But you've told us that already know about this.

- The algorithms used in your program. This part is the same for Java as for C++ or any procedural or object oriented programming language.

So I think that what you need is a good program design to avoid deadlock.

Cheers.

Dian
 
I agree with Dian. Java provide you the mechanism to avoid deadlock and ensure mutual exclusion. It is up to the programmer to utilize it. You are talking more of a design problem rather than an implementation problem
 
To avoid dead lock:
[ul]
[li]Always aquire in same order, if you require more than one resource.
[ul]
[li]Get A then B if need A and B any time durring code[/li]
[li]Get just A if need just A.[/li]
[li]If you have B, and need A, let go of B (if it is safe to do so to maintian mutal exclussion)[/li]
[li]You can have either A or B alone at anytime, as long as you don't need to grab the other.[/li]
[/ul][/li]
[li]Release in oppiste order than aquire (Java forces you to this -- if your just using sychronize).[/li]
[li]Increase in number of lock = increase probability of accidental dead lock. Decrease in the number of locks means more things locking on one item and slower speed. Here you have the age old trade off between useability and security.[/li]
[li]Every shared mutable state requires protection, any protection aquired needs to be released in a timely fashion ; but only after the whole operation with the mutable state is completed.[/li]
[/ul]

Sychronization is the easiest thing and the hardest thing to do in computers. To make sure that no more than one process/thread is changing something, don't let anyone touch it. To make sure that everyone has access to something always don't protect it. The Zen in programming is finding the middle ground between the two that does something useful. Use these tips well grasshopper.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top