Describe the process of thread synchronization and how you would prevent race conditions or deadlocks in a multi-threaded system.