One of the strategy to avoid deadlock situation in Java Multithreading is using timeout.
Suppose, one thread has acquired lock on one resource and now waiting for lock on another resource. After certain time period if it can not acquire lock on resource2 then it should stop waiting for lock on resource2. Also it should release lock on resource1. Thus deadlocks will be avoided.
But how to implement it in Java ? How to explicitly “release” lock ? How to define timeout to wait for lock.
What is exact java commands and syntax. Any hello-worldish example please ?
Here is a contrived example with 2 locks and 2 threads that try to acquire them in different orders. Without the timeout, the code would deadlock.