Causes of thread safety problems:
- Multiple threads are manipulating the shared data
- There are multiple thread codes that manipulate shared data
When a thread executes multiple code processes that share data, other threads participate in the operation.
Causes a thread-safety problem to occur.
Solution Ideas:
is to encapsulate the thread code that has multiple operations sharing data, and when the thread is executing the code, the other threads are not allowed to participate in the operation.
It has to be the front line. After the code has been executed, other threads can participate in the operation.
Format of the synchronized code block:
Synchronized (object)
{
Code that needs to be synchronized;
}
Benefits of synchronization:
Resolves a thread-safe issue.
Disadvantages of synchronization:
The relative reduction in efficiency, because threads outside the synchronization will judge the synchronization lock.
Prerequisites for synchronization:
You must have multiple threads in the synchronization and use the same lock.
"Thread synchronization has 2 representations, synchronous code blocks, synchronization functions"
Producers, consumers.
Multi-producer, multi-consumer problem.
If the flag is judged only once, it causes the thread that should not run to run. A case of data error occurred.
While judging the flag, resolves if the thread gets execution right after it is run!
Notify: Only one thread can be awakened, if this party wakes up the party, it is meaningless. And while judging the token +notify will cause a deadlock.
notifyall resolves an issue in which the thread of this party will surely wake the other thread.
The jdk1.5 later encapsulates the synchronization and locks into objects.
And implicitly defines the action lock to the object,
The implicit action is turned into a display action.
Lock interface: A synchronous code block or synchronization function appears instead. Converts the implicit lock operation of a synchronization into a real-world lock operation.
and more flexible. A lock can be combined with multiple sets of monitors.
Lock (): Gets the lock.
Unlock (): Releases the lock, which is typically required to define a finally code block.
Condition interface: An alternative to the wait Notify Notifyall method in object is present.
These monitor methods are individually encapsulated and become condition monitor objects.
Can be combined with any lock.
Await ();
Signal ();
Signalall ();
Wait/wake mechanism.
The method involved:
1,wait (): The thread is frozen and the wait thread is stored in the thread pool.
2,notify (): Wakes a thread in the thread pool (any).
3,notifyall (): Wakes all threads in the thread pool.
All of these methods must be defined in synchronization.
Because these methods are the methods used to manipulate the thread state.
It is important to be clear on which locked thread the operation is.
Why is the method for manipulating threads wait notify Notifyall defined in the object class?
Because these methods are the methods of the monitor. A monitor is actually a lock.
A lock can be any object, and the way an arbitrary object is called must be defined in the object class.
Wait and sleep difference?
1,wait can specify time or not.
Sleep must specify a time.
2, in the synchronization, the CPU execution and lock processing is different.
Wait: Release the execution and release the lock.
Sleep: Releases the execution and does not release the lock.
Baidu search for thread synchronization related knowledge
Java Thread Lock