from main memory, a value of 5, stores it in its own thread space, performs an add 1 operation, and a value of 6. At this point, the CPU switches to thread B execution and reads the value of the variable i from the master-slave memory. Since thread A has not had time to write the result of adding 1 back to main memory, thread B has read I from main memory, so the variable i value read by thread B is still 5.It is equivalent to thread B reading data that is obsolete, which results in thread inse
Java multi-thread synchronized
First, let's look at the next scenario. A cinema sells tickets for four windows at a time. There are only 100 tickets in total for this movie. Check the actual code.
Package cn.com. thread; public class TestThread {public static void main (String [] args) {SellTicketThread t = new SellTicketThread (); new Thread (t, window 1 ). start (); new Thread (t, window 2 ). start (); n
Data synchronization need to rely on the lock, the synchronization of the lock who depends on? The answer given by synchronized is that it relies on the JVM at the software level, and lock gives the option to rely on special CPU instructions at the hardware level, and you may further ask: How does the JVM's bottom layer implement synchronized?The JVM referred to in this article refers to the 6u23 version of
The Java language keyword that, when used to decorate a method or a block of code, guarantees that at most one thread executes the code at the same time.First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current th
comparison of these types of locks:
Lock
Advantages
Disadvantages
Applicable scenarios
Biased lock
Locking and unlocking does not require additional consumption, and the execution of a non-synchronous method is less than the nanosecond-level gap.
If there is a lock contention between threads, additional lock revocation consumption is brought.
Applies to only one thread to access the synchroniz
are acquired, they must be released in reverse order . The synchronized mechanism's release of the lock is implicit, and the lock is freed as long as the thread runs out of the synchronized statement block range. The lock mechanism must explicitly invoke the Unlock () method of the lock object to release the lock, which provides the possibility that acquiring an
Reference article: http://blog.csdn.net/chen77716/article/details/6618779There are two kinds of locking mechanisms in Java: The synchronized and Lock,lock interfaces and their implementation classes are JDK5 additions, and the author is the famous concurrency expert Doug Lea. This article does not compare synchronized with lock which is inferior, just introduces
Synchronized Java language The simplest thread concurrency synchronization syntax, the source of the fastest understanding,The Resouce class represents concurrent resourcesUse Method 1. Lock A variable directly, such as lock in the code, note that lock must be static here. What happens if it's not static? Public class Resource { private static object lock = new
what if there is a thread that is angry and doesn't want to wait? Sorry no, we need to wait all the time. On the other hand, the release sequence of the synchronization lock is also very fixed, must be locked in the reverse order, very not handsome and so on ... But our reentrantlock is not the same, not much to say first read the code./** * @author lw * */public class Test extends Thread{ private static ReentrantLock lock =new ReentrantLock(); public void run() { try{
keyword, so access is synchronized, there is no problem, if the 1 to change to a common method. There may be a problem.1 If a thread holds a lock on an object, thread B can call a method that is not decorated with synchronized, and wireless waits2 If a thread holds the lock on the object, thread B also calls the synch
current class is locked, and when the static function is accessed, all the threads that want to access the function enter the blocking queue④synchronized is said to be able to decorate a class,class ClassName { publicvoid method () { synchronized(ClassName. Class) { // todo } }}However, the experiment seems to be not mutually exclusive
1 IntroductionIn multi-threaded concurrent programming, synchronized has always been an elder role, and many people will call it a heavyweight lock, but with the various optimizations that Java SE1.6 has made to synchronized, in some cases it is not so heavy, this article details the Java The biased and lightweight loc
Java synchronized is mainly used to control thread synchronization, in the middle there are a lot of small details, knowledge, here I simply tidy up, make a record. Primarily for methods and code block controlLet's talk about method control.Simulate bank deposits and withdrawals, create an account class, two attributes, a name and accout. First save 1w go in, wit
object multiple threads can start mutually exclusive effect, if it is different objects can not be mutually exclusive effect.Import Java.util.concurrent.timeunit;public class SynchronizedDemo2 {public synchronized void Synchronizeddemo () { try {TimeUnit.SECONDS.sleep (2);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Test1");} public void Test1 () {Thread thread = new Thread () {public void run () {Synchronizedde
In Java, synchronized is used to represent synchronization, and we can synchronized to modify a method. You can also synchronized to modify a block of statements inside a method. So what's the difference between static and non-sta
I. the synchronized keyword has two scopes:1. is an object instance, synchronized aMethod () {} can prevent multiple threads from simultaneously accessing the object's synchronized Method (if an object has multiple synchronized methods, as long as a thread accesses one of th
Usage of synchronized in Java (GO)"Multi-threading and multi-process (1)--talking about threads and processes from the perspective of an operating system" describes in detail the threads, process relationships, and performance in the operating system, which must be understood as a basis for multithreaded learning. This article goes on to talk about an important conceptual
Synchronized lock:
1. All the locks obtained by synchronized areObject lock,Instead of locking a piece of code or method.
Synchronized locks the instance object of this method. If multiple threads accessSame object
The synchronized
It's possible to synchronize both an entire method and a sections of code within a method, and you could wonder which one you should use. To understand which are appropriate in a given situation, it's important to consider what synchronization really provides.Stated simply, synchronization allows you to prevent multithreaded execution of certain portions of a multithreaded applic ation. In other words, sync
In Java, the synchronized keyword can be used as a method modifier orCodeBlock, for example:
Public synchronized void dosomething ()
Synchronized (semaphore ){...}
The Methods modifier has the following advantages:
The synchronization policy is explicit and can
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.