Original: http://blog.csdn.net/zheng548/article/details/54426947Difference One: API levelsyschronized useThe synchronized can be modified or decorated with code blocks.The synchronized modifies the method as follows:Synchronized modifies a method, this method is called synchronous method public synchronized void Test () { //method body}When
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; swi
.
public class Accountingsync implements Runnable {
static int i = 0;
public static synchronized void increase () {
i++;
}
@Override public
Void Run () {for
(int j = 0; J thread t2 = new Thread (new Accountingsync ());
T1.start ();
T2.start ();
T1.join ();
T2.join ();
System.out.println (i);
}
}
3. Wrong lock
From the above example, we know that
Java thread security: the usage of the synchronized keyword is unknown today. The synchronized keyword is used.CodeThread Synchronization is safe.
Tested. It is totally wrong. Synchronized must be correctly used to ensure real thread security... Although I knew this method, I always thought that I was using the wrong
"About the content"This article mainly introduces how to use the Synchronized keyword in Java to implement the mutex of the thread correctly."Capacity Requirements"At the very least, we have mastered the basics of Java syntax, basic object-oriented knowledge, and created and started threads.BodyAbout the use of synchronized
In fact, more than 90% of my basic java knowledge comes from Thinking in Java. For the synchronized keyword, you just browsed it and probably didn't take it into detail. This keyword has never been used. Yesterday I saw the Thinking in Patterns with the Observer mode in Java. I checked the source code of the Observable
block the thread.
2, from the perspective of memory visibility, volatile read equivalent to lock, volatile write equivalent to unlock.
3, synchronized can guarantee the visibility, but also to ensure atomicity, and volatile can only guarantee visibility, can not guarantee atomicity.
code example: PackageOrg.apple.thread;ImportJava.util.concurrent.locks.Lock;ImportJava.util.concurrent.locks.Ree
guarantee the visibility of the data and cannot be used for synchronization because multiple threads concurrently accessing volatile modified variables are not blocked.Synchronized not only guarantees visibility, but also guarantees atomicity, because only the thread that obtains the lock can enter the critical section, guaranteeing that all statements in the critical section are executed. Blocking occurs when multiple threads scramble for synchronized
gives us a lot of flexibility. For example: If a, B2 thread to compete lock, a thread get lock, b thread wait, but a thread this time really have too many things to deal with, is not return, B thread may be able to wait, want to interrupt themselves, no longer wait for this lock, to deal with other things. This time Reentrantlock provides 2 mechanisms, first, the B thread interrupts itself (or another thread interrupts it), but Reentrantlock does not
Java Sync Lock (synchronized)in Java, synchronized is a lock, he can lock a method, can also lock a method, I wipe, in fact, these two things are the same. Block is not a method without a name, the method is a name of the block. This article uses blocks to test. The so-called lock, is the atomic operation, the locking
Every object in Java has an object lock, and synchronized is going to get this lock, see the example belowImportJava.util.Random; Public classmydata{ Public synchronized voidincrement () { for(inti = 0; I ) { Try{Thread.Sleep (NewRandom (). Nextint (200)); } Catch(Exception e) {e.printstacktrace (); } System
= 0, then there is an operation a++, this variable a has visibility, but a++ is still a non-atomic operation, that is, the operation also has a thread safety problem.Volatile, synchronized, and final implementation visibility in Java.Atomic nature: atoms are the smallest units in the world and are indivisible. such as a=0; (a non-long and double type) This operation is indivisible, then we say this operation is atomic operation. Another
execute synchronized block code in one thread at the same time.
Test code:
Package demo. thread; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors;/** consumption Number */public class evenchecker implements runnable {private intgenerator generator; private final int ID; Public evenchecker (intgenerator g, int ident) {gener
multithreading synchronization problem with synchronized keyword in Javain Java, multi-threading synchronization relies on the object lock mechanism, the Synchronized keyword is the use of encapsulating object locks to achieve mutually exclusive access to shared resources. The following is a simple example of multithre
class, the lock it obtains is the same lock on the class, all objects of that class.B. Each object has only one lock (lock) associated with it, and whoever gets the lock can run the code it controls.C. Achieving synchronization is a costly overhead, and may even result in deadlocks, so avoid unnecessary synchronization controls as much as possible.
retouch Content
Lock Type
Example
modifying code block
Learn a little bit of programming every day PDF ebook, video tutorial free download:Http://www.shitanlife.com/code1. How to useSynchronized is the most common way in Java to ensure thread safety, and synchronized has three main functions:
Ensure thread-mutually exclusive access code block, at the same time only one method can enter the critical section
Ensure that changes to shared variables ar
In the project development, may encounter Java multithreading, in order to ensure the normal business data, must be coupled with the lock mechanism, commonly used to deal with synchronized keyword, the current JDK version of synchronized has done a very good optimization, we do not have to consider its performance , but in the actual use, often due to improper ha
function, every incoming thread will wait in the queue for the first case. There are two lock mechanisms for ReentrantLock, which ignore the interrupt lock and respond to the interrupt lock, this gives us great flexibility. For example, if A and B2 threads compete for the lock, thread A gets the lock and thread B waits, but thread A has too many things to handle at this time, that is, it never returns, the B-thread may be unable to wait. If you want
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.