How to write deadlock and resolve in java