How does multithreading work in Java, and how do you prevent thread interference and data inconsistency?