Http://hi.baidu.com/chaletli/item/beb128f39dccd413d7ff8cf5
Note the following when using the synchronized keyword:
1. the synchronized keyword cannot be inherited.
Although synchronized can be used to define a method, synchronized is not part of the
Before Java1.5, synchronized should be the most common Java support Concurrency tool. So, how did synchronized do that, starting with java1.0, each object in Java has an internal lock. If the method of a class is decorated by the Synchro
that our shared resources can only be used by one thread at a time. Once this resource is used by a thread, other threads will not have the right to use it. Java provides two methods for Synchronous mutex access: synchronized and Lock.2. synchronized for mutex access (synchronous method or synchronization block)
Mutex
Java has the following methods to handle concurrent access to code blocks:First, the keyword synchronizedSecond, lock (Lock Object, condition object)Java provides an internal lock for each object. After the keyword synchronized is added to the method definition, the object lock will protect the entire
always read from primary memory, but this is only before JDK 1.2, the current virtual machine implementation makes the memory pattern more complex and optimized, and this trend will only continue. In other words, volatile based on memory patterns may become meaningless because of the continuous optimization of memory patterns.
The use of volatile is limited, it only solves the problem caused by the memory mode, and can only be used in the automic operation of the variable, that is, the
problem.TraditionalThreadSynchronized. java: creates two threads and executes the output method of the same object.[Java]Public class TraditionalThreadSynchronized {Public static void main (String [] args ){Final Outputter output = new Outputter ();New Thread (){Public void run (){Output. output ("zhangsan ");};}. Start ();New Thread (){Public void run (){Output
Java keywords volatile and synchronized, javavolatile keywords
Volatile is a variable modifier, while synchronized acts on a piece of code or method. The code for the following three get methods:
1 int i1;2 int geti1() {return i1;}3 4 volatile int i2;5 int geti2() {return i2;}6 7 int i3;8
,synchronized's wider function range (synchronization mechanism).3.synchronizedSynchronized keyword is the use of the lock mechanism of Java automatic implementation, there are generally synchronous method and synchronous code block two ways. All objects in Java automatically contain a single lock (also known as a monitor), and when any of its
1. abstract and static
(What)
Abstract: It is used to declare abstract methods. abstract methods do not have a method body and cannot be called directly. They must be used after the subclass overriding.
Static: Used to declare static methods. Static methods can be called by classes and objects.
(How)
Static and abstract cannot be used at the same time
(Why)
The static declaration method indicates that this
Java multi-thread synchronized and volatile comparisonOverview
When performing multi-threaded concurrent processing, you often need to perform visibility access and mutex synchronization operations on resources. Sometimes, we may know from our predecessors that we need to perform volatile or synchronized keyword modification on resources. However, we do not know
Usually to synchronized this key word does not care too much, to it's understanding to stay in a rough turn over the state of Baidu Encyclopedia, Baidu Encyclopedia to its explanation is:"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
We can synchronize specific static or non-static methods by synchronized blocks. To implement this requirement, you must define a class variable for the methods of these attributes, and then enclose the code of these methods in a synchronized block and pass the class variable as a parameter to the synchronized block.The following code demonstrates how to synchron
synchronous mutex access, which means that our shared resources can only be used by one thread at a time, and once that resource is used by the thread, other threads will not have the use rights. In Java, there are two ways to achieve synchronous mutex access: synchronized and lock.second, mutually exclusive access to the synchronized (synchronous
in concurrent programming, multi-threaded concurrent access resources are called critical resources, when multiple threads concurrently access the object and require the operation of the same resources, split the atomic operation there may be inconsistent data or incomplete data, in order to avoid the occurrence of this situation, We will take a synchronization mechanism to ensure that only one thread is allowed within a method at a certain point in t
the output result to be zhangsanlisi, which is a thread synchronization problem. We hope that the output method will be switched to the next thread after being fully executed by a thread, in Java, synchronized is used to ensure that a piece of code is mutually exclusive during multi-thread execution. There are two usage methods:
1. Use
We can use the synchronized block to synchronize specific static or non-static methods. To implement this requirement, you must define a class variable for the methods of these features, then enclose the code of these methods in the synchronized block, and pass the class variable as a parameter to the synchronized block. The following code demonstrates how to syn
definition of happens-before in the art of Java concurrent programming:
In JMM (Java Memory Model), if the result of one operation needs to be visible to another operation, there must be a happens-before relationship between the two operations. The two actions mentioned here can be either within a thread or between different threads.
Volatile memory semanticsFor multithreaded programming, eac
Http://www.cnblogs.com/xrq730/p/4853578.htmlSynchronized can also be applied to static methods, and if so, it represents the class lock that corresponds to the current. java file . Take a look at the example and note that PRINTC () is not a static method:PublicClassthreaddomain25{PublicSynchronizedStaticvoidPrintA () {Try{System.out.println ("Thread name is:" + thread.currentthread (). GetName () + "in" + system.currenttimemillis () + "Enter Printa (
Synchronous static methodsSynchronized can also be applied to static methods, and if so, it represents the class lock that corresponds to the current. java file . Take a look at the example and note that PRINTC () is not a static method: Public classthreaddomain25{ Public synchronized Static voidPrintA () {Try{System.out.println ("Thread Name:" + Thread.CurrentTh
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.