Explain the lifecycle of a thread in Java and how you prevent deadlocks in a multithreaded environment.