1. Why do I overwrite the run () method?
The thread class is used to describe threads. The class defines a function that stores the code that the thread wants to run, and the function of the store is implemented by the run () method.
2. The running state of the thread:
The thread is created, the start () method is called to enter the running state, and if the call to sleep (time) or the Wait () method thread enters a frozen state, but still exists, you can try the Notify () method to wake the thread;
When the thread finishes running or calls the Stop () method, the thread dies.
3. Security issues with multithreading
WORKAROUND: A statement that shares data on multiple operations can only be done after one thread has finished executing, and other threads are not allowed to participate during execution.
YoY code block: Synchronized (object) {code to synchronize}
The above object is actually a lock, the thread that holds the lock can execute code in the synchronous code block, the thread that does not hold the lock even gets the CPU execution right, because the lock object is not acquired
Prerequisites for synchronization: Two or more than two threads
Multiple threads must use the same lock object
4. How do I see which code needs to be synchronized?
- Identify which code is multithreaded to run code
- Identify what is shared data
- Identify which statements in multithreaded run code work with shared data
Dark Horse Programmer--some summary of threads