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