Explain how multithreading works in Java, and detail how you would prevent deadlocks in a multi-threaded application.