A thread is a different path in a program
Example 1
Only one path
Each branch is a thread
In fact, a computer can run only one process at a time, but because the CPU is running fast, we feel like we're working at the same time.
Two ways to create a thread
Example 1:
Example 2:
Inherit from thread
Use interfaces as much as possible
Thread State Switching
Basic methods of threading control
Sleep/join/yield method
Test 1
Test 2
Merge to a Main method
Priority of threads (not all)
Example
Example
How to stop right
Currentthred Get the current thread
Thread synchronization:
Problem description
There's 3000 bucks, you and your wife are going to get the money. You go to the counter to fetch your wife to the ATM machine to pick up the money to check whether it is enough money and finally found enough for you and your wife to take 2000 dollars account changes to 1000, and then you took 4000? This is not possible.
Example
Lock the current object
Example
Interview questions
Operation result is 1000
The lock is just locked in the method.
Producer Consumer Issues
Make steamed bread and eat steamed bread
Notify Wake up Wait
The difference between wait and sleep
Wait is the method of the object, and sleep is the thread.
Wait
Something else is accessible.
Sleep is inaccessible
Java review of multi-threading