Explain the concept of thread safety. What is a deadlock, and how do you prevent it in a multi-threaded application?