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
The following content is mainly summarized from the "Java Multithreaded Programming core technology", the update is not scheduled to supplement.First, the state of the threadIn Java, there are 6 types of threads in the state: NEW, RUNNABLE, BLOCKED, waiting, timed_waiting, TERMINATED. The relationships between the States can be expressed as:Ii. introduction of common methods1. The difference between Thread.
, 10);
}
public static void Main (string[] args) {
Test (New Evengenerator ());
}
}
Analysis: If the class that produces even numbers is not synchronized, then the test program will appear odd to cause the program to exit. 2, volatile indicates atomicity, visibility.For variables shared between multiple threads, each thread has its own copy, and when thread 1 changes the value of the variable, other threads do not immediately know th
the JVM in making the decision, it is not simply based on a condition to make judgments, but according to a number of bars. But because the judgment condition is too many, if said that may affect the Java promotion, may also be because of the intellectual property protection reason. Sun gave a no guarantee and mixed it up. Understandable. But I believe these uncertainties are not entirely uncertain. Because the computer itself is running according to
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
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 multithreading synchronization problem, we want to add
1, for synchronized method and synchronized block, when one thread accesses, other threads can access the non-synchronized method or block of this class, and cannot access other methods or blocks with synchronized.Examples are as follows:Synchronized Block:public class Threa
executes their own.Let's take a look at the following exampleImport Java.util.Random;public class mydata{public synchronized void increment () {for (int i = 0; i try {Thread.Sleep (New Random (). Nextint (200));} catch (Exception e) {E.printstacktrace ();}System.out.println (Thread.CurrentThread (). GetName () + ":" +i);}}public static synchronized void decrement () {for (int i = 0; i try {Thread.Sleep (Ne
Java Thread Series (v) synchronizedsynchronized lock re-entry
The keyword synchronized has the ability to re-enter the lock, that is, when using synchronized, when the thread waits for the lock on an object, it can again get the lock on the object when it is requested again. Release the lock when an exception occurs.
S
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:From the result of the run, the call to the PRINTC () method ( non-static ) and the call to the Printa () method, Printb ()
The fundamental purpose of the synchronization is to control the proper access to competitive resources, so as long as access to competitive resources is guaranteed to only one thread at a time, Java introduces a strategy for synchronizing code fast to improve performance.
On the basis of the last example, the Oper method is changed, and the synchronization method
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
1.synchronized the basis for synchronizationEach object in Java can be used as a lock, with 3 different manifestations.1. For the normal synchronization method, the lock is the current instance object.2. For the static synchronization method, the lock is the class object of the current classes.3. For a synchronous
In the "Java" thread concurrency, mutual exclusion and synchronization (click Open link) in the operating system through the operation of the original method of semaphore control, complete the thread of mutual exclusion and synchronization, say a digression, in fact, this signal volume algorithm, is the famous Dijkstra created, that is, the data structure, The shortest path algorithm, Dijkstra algorithm and
Java's multi-thread support and synchronization mechanism are favored by everyone. It seems that using the synchronized keyword can easily solve the problem of multi-thread shared data synchronization. What exactly? -You must have an in-depth understanding of the role of the synchronized keyword. In general, the synchronized keyword can be used as the modifier of
Java synchronized same object multi-instance thread security
Synchronized is the simplest thread concurrency synchronization syntax in java. The source code is the fastest to understand,
The Resouce class indicates concurrent resources.
Method 1: directly lock a variable, su
counter value may be 0. Because this does not ensure that thread a's change to counter is visible to thread B, unless the programmer has established a previous (happens-before) Relationship between the two threads.
There are several actions on the relationship before the creation. One of them is synchronization, as we will see below.
We can see the two actions of the relationship before the creation.
When a statement calls thread. Start, each statement that has a lifetime relationship with
Volatile: Only guarantee the visibility of shared resources, any changes are written in main memory, all threads can be seen immediately, for the new value does not depend on the old value of the case.Synchronized: Ensures operational atomicity consistency and visibility.The difference between volatile and synchronized: Volatile only causes variables to be known to other threads as soon as the value changes, and does not guarantee the sequential exec
Recently, when doing projects, encountered a lot of synchronization problems.
JAVA provides us with a convenient way to solve the problem of multi-threaded synchronization synchronized keyword
have been very puzzled synchronized how to do the synchronization of what is the principle of implementation?
1 Synchronized--
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.