1. Synchronized definition
The Java language keyword, which can be used to lock objects and methods or blocks of code , when it locks a method or a block of code, at most one thread at a time executes the segment code. When two concurrent threads access the same object in the same lock synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block. However, when a thread accesses one of the lock code blocks of object, another thread can still access the non-locking code block in the object.
Note : Synchronized cannot decorate a single object, synchronized should be a combination of an object + method or an object + code block .
Object + method : The direct modification method, which does not reflect the object, but in fact, the class instance is locked object.
Public synchronized void A () {s ("a");}
Object + code block : This is more obvious, this is the instance of the class, and the content in {} is a block of code.
public void A () {synchronized (this) {s ("a");}}
The above two examples are equivalent.
2, make an analogy to understand
Synchronized is actually a game.
Game scene: A big House (object), there are several rooms (methods), some are locked, some are unlocked
Game characters: Administrator (JVM), player (thread)
Game rules:
(1) no locked room can be used at any time, or can be used by multiple players simultaneously
(2) The locked room can only be used by one player, and only one key will open.
(3) The key can be taken with the administrator, if there are multiple players who want to get the same key, the administrator will randomly give one of the players
(4) After the player has finished the room must return the key to the administrator (even if want to use again, also must first also the administrator, then with the administrator wants; if other players also want this key, then will randomly give one of them)
3. Where to use Android
First of all, from the definition can know,synchronized only in the multi-threaded scene has the meaning of existence.
so in Android, where multithreading is used, a common example is GridView loading local pictures asynchronously .
Because loading pictures is time-consuming, it is unwise to load multiple images in the same thread at once, and there are many local images.
So we'll consider using the thread pool to load the image.
Then there is a problem with the thread being more natural: What is the problem when the same picture may be loaded by multiple threads?
In fact, this is a problem that inevitably arises; swipe up quickly Picture List (in the case of very many pictures), and then quickly down Slide , if the previously loaded picture has not been loaded, there will be a new thread to load the picture again, not error, but the same picture may be loaded multiple times, the interface will have the phenomenon of a flash of pictures , and Kinda Wasting Memory the.
And at this time, if you use the synchronized keyword, you can easily solve the ~ ~ As to how to design code, here do not mention, attention to lock the object must be the same on the line.
Remind again : Lock different Objects It doesn't make any sense, code like the following is meaningless!
public class Test implements runnable{@Overridepublic void Run () {synchronized (the) {for (int i = 0; i < 5; i++) {Sys Tem.out.println (Thread.CurrentThread (). GetName ());}}} public static void Main (STRING...ARG) {new Thread (new test (), "A"). Start (); New Thread (New test (), "B"). Start ();}}
It should be written like this:
Test test = new test (), new Thread (test, "A"). Start (); New Thread (Test, "B"). Start ();
Java Keyword Synchronized Learning notes