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