Thread Safety:
In the case of multithreading, data errors are not caused by operations between threads.
Thread synchronization:
The same resource, which may be manipulated by multiple threads at the same time, can result in data errors. This is a phenomenon, but also a problem, and the study of how to solve
The related work of this kind of problem is called thread synchronization.
In Android, the means of handling thread synchronization is: Lock
Generally divided into fair lock and non-fair lock:
Synchronized (internal lock, mutex):
Synchronized is the thread synchronization mechanism provided by the JVM, and if there is a problem, the JVM can catch the exception and release the resources, the implementation mechanism needs to view the JVM source code
Features of synchronized use:
Lock interface:
These implementations of the lock interface are implemented and encapsulated through the Java language, and the implementation mechanism can be learned by looking at its source code.
Lock instance locking and unlocking the approximate process:
When a resource is locked, the thread that accesses it is added to the wait queue when
Internal lock (mutex) Reentrantlock
Read/write Lock
Synchronized
1.synchronized action on Method: Lock Object
2.synchronized function in code block: Lock code block only
3.synchronized (This): Object lock
4.synchronized (object): Member lock
Summary synchronized:
The object can be locked: synchronized (this), synchronized (class), synchronized methods, all of the objects
Resources cannot be accessed by other threads
Member Lock:
Synchronized (obj) {}, code within the code snippet cannot be accessed concurrently.
Object Lock too coarse
Usually the use of synchronized is sufficient, the resource competition is very intense situation, the performance of synchronized will be reduced by dozens of times times before using
Reetrantlock to complete complex business requirements. (not fair Lock)
Using a Java-encapsulated lock object can be more flexible to meet business needs, such as the ability to set lock waiting time, fairness and so on, while using synchronized
You can only use the three methods of the synchronization monitor to manipulate a thread
Thread Communication:
Synchronized as an object lock, through this, you can call three methods of communication between threads
Synchronized as a member, through (obj), call three methods, where the locked thing is called the synchronization Monitor. (this,obj)
Wait: The current thread goes into a wait state until another thread calls the synchronization Monitor's Notity method or the Notityall method to wake.
Calling wait is supposed to release the sync Monitor
Notify: Wakes a single thread waiting on this synchronization monitor. If all threads are waiting on this synchronization monitor, it is randomly chosen to wake up one of the threads.
The wake-up thread can only be executed if the front-thread discards the lock on the synchronization monitor (using the Wait method)
Notityall: Wakes up all the threads waiting on this sync monitor. A thread that wakes up can be executed only when the front-thread discards the lock on the synchronization monitor.
Threading Model:
Message loops
Message Queuing
Handler
Thread
UI Thread
The threading model for Android:
The UI thread has a message queue, a message loop, and a handler.
A normally created background thread object is a one-time task to complete a job.
How to establish a connection between them?
In general, after starting a non-UI thread for processing time-consuming work, a mechanism is required to notify the UI thread that the work is done,
This mechanism is a message.
After the background thread has finished working, send a message to the thread to notify the target thread that said: "I'm done," and this
How is the message sent over? Through the handler object. Send a message to the thread's handler if you want to send a message to that thread.
(provided that the target line threads the ability to process the message)
Comprehensive:
When the background thread finishes working, a message loop is sent to the UI thread's message queue through the handler object of the UI thread, which is passed through the interface.
This message is taken out of the message queue, and this message is processed and the sex is done.
For a background thread:
If you want the thread to have Message Queuing and message loops, you need to First call Looper.prepare () in the threads to create the message queue, and then call Looper.loop () to enter the message loop. As shown in the following example:
Class Looperthread extends Thread {
Public Handler Mhandler;
public void Run () {
Looper.prepare ();
Mhandler = new Handler () {
public void Handlemessage (Message msg) {
Process incoming messages here
}
};
Looper.loop ();
}
}
This way your thread has a message processing mechanism, and the message processing is done in handler.