Design a thread-safe cache mechanism in Java and explain how you would handle synchronization to prevent deadlocks.