In this, really thank the Zhang Xiaoxiang podcast teacher, although has left, but still want to thank. Multithreading this piece helps a lot. (Watch a video of public lessons)
In general, I can write a single-threaded version of the program, but it is important to consider clearly, will be a race condition of the resources written into the same class.
Then consider if there is a race condition, then add synchronized if you need to keep synchronous communication with this.wait () and This.notify (), and so on, why use while because the thread may wake up, not be awakened, but wake up. You can judge it again with while. You can't use IF.
Then see if you can use the lock to further optimize, synchronous communication, the use of condition to optimize, the benefits of the lock can be read and write lock, this is really bad. Admire.
Tips on multi-threaded writing--Thoughts on the video of Zhang Xiaoxiang podcast teacher