+ 1
Any program example to create a deadlock situation in Java? Solving ideas?
Can anyone give an example of a deadlock situation in java. Write a small java program as an example. Thank You!
10 Réponses
+ 14
If you need to use multiple locks, do it in a well defined order. Simple as that to be said, often very hard to do/find the mistake/test.
Generally spoken: be careful, be sure you know what you're doing if you need multi threading. The snychonized keyword should only be used after you double-checked that you need it and you are sure it works as expected.
+ 13
You can really simply create a deadlock if you use the synchronized keyword carelessly. Example and more on concurrency can be found in the Oracle tutorials:
https://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
+ 3
here you go: https://www.javatpoint.com/deadlock-in-java
+ 2
So in https://www.javatpoint.com/deadlock-in-java
to prevent the deadlock we
should alter the resources in thread 2.....So, if thread 1 acquires lock on resource 1 , thread 2 will not proceed until thread 1 releases resource 1 lock, same way thread 1 will not be blocked even if thread 2 holds resource 2 lock because now thread 2 will not expect thread 1 to release resource 1 lock to proceed further. Am i right? Thank You all. 🙂
+ 1
@Tashi N How to fix such situations?
+ 1
And the best way to solve it is just by preventing a deadlock to occur..
+ 1
@Tashi N Thank You ☺️
+ 1
@Jan sorry , am little weak in English.
0
@Boem how can you fix that? Changing sleep? Or by changing the order of resource?
0
Search for posts like this in google: https://howtodoinjava.com/core-java/multi-threading/writing-a-deadlock-and-resolving-in-java/