1. Synchronized principle
In Java, each object has and has only one synchronization lock. This also means that the synchronization lock is dependent on the object and exists.
When we call the Synchronized method of an object, we get a synchronized lock on the object. For example
writing a synchronization method or a synchronized block, and the JVM automatically acquires the lock or releases the lock when the monitoring area is manipulated.
Example 1
Let's take a look at the first example, in Java, the critical section of the same object, at the same time only one allowed to be accessed (bot
point. But this creates a problem because thread a already holds the lock on the object and is requesting a lock on the object, so that thread a waits for a lock that will never be acquired.
And because synchronized and lock have the ability to be reentrant, so this phenomenon will not occur. 2. Can break lockInterruptible Lock: As the name suggests, is the lock that can be interrupted accordingly.In Java,
called on one lock object; otherwise, the following exception occurs:Java. lang. IllegalMonitorStateException: current thread not ownerThe following code is valid: the first example calls this. policyall (); this is the lock object.[Java]Public synchronized void sendNotification () throws Exception {Policyall ();}Synchronize
threads then It ' s ideal to use Concurrent Hashmap. Best example is Producer Consumer which handles concurrent read/write.So what does the Thread-safe Map means? If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it to must be synchronized externally avoid a n Inconsistent view of the contents.How?There is ways we could
Concurrent Series Reference article http://www.cnblogs.com/skywang12345/p/3323085.html#3907193Synchronized principleIn Java, each object has and has only one synchronization lock. This also means that the synchronization lock is dependent on the object and exists.When we call the synchronized method of an object, we get the synchronization lock for that object. For exam
IntroductionAs we said in the previous article, volatile is guaranteed visibility, ordering, and "partial" atomicity through the lock command. However, in most concurrency problems, it is necessary to ensure the atomicity of the operation, volatile does not have this function, then need to use other means to achieve the purpose of thread safety, in Java programming, we can through the lock, synchronized key
Reference: http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.htmlThe root cause of the multithreading problem:In a multithreaded environment, when a change is made to an object, a thread a changes the variable, but does not change the finish, and the CPU is snatched by another thread B, then a is no longer executed, resulting in inconsistent data behavior. For the above citation in the bank withdrawal deposit example, originally saved 100 t
synchronized is not available, thread A will need to reapply for the lock at this point. But this creates a problem because thread a already holds the lock on the object and is requesting a lock on the object, so that thread a waits for a lock that will never be acquired.
And because synchronized and lock have the ability to be reentrant, so this phenomenon will not occur.
2. Can break lock
Interruptible L
Transferred from: http://blog.csdn.net/zyplus/article/details/6672775In Java, there are no similar methods related to PV operations, process mutexes, and so on. Java's process synchronization is achieved through synchronized (), it should be explained that the Java synchronized () method is similar to the operating sys
The concept of lock is familiar to everyone, when a thread A has already held a lock when thread B tries to enter a code segment protected by this lock. It will be blocked. The operation granularity of the lock is "thread", not the call. Each Java object can be used as a mutex (synchronized) that implements synchronization, which is called a built-in lock. When a thread enters a synchronous code block or me
all programs will not have to be added by themselves. NBSP After introducing the principle of Java synchronization, let's go to the point where we'll talk about the use of synchronized, and the other synchronizations will be covered in later chapters. To run an example first try.
Package thread_test;
/**
* Test Extended thread class implementa
, another thread can be executed on the CPU, so that the time to execute down is 6s. So next look at how to solve this problem with synchronized code block, I wrote an example, as follows: public class Task_synchronized {private String Getdata1;private String GetData2; //no synchronized adornments public void dolongtimetask () {try {System.out
Synchronized is a keyword in Java that is called a built-in lock or monitor lock in concurrent programming. When you use it to decorate a method or a block of code, you can guarantee that at most one thread at a time executes the code.Java's built-in lock is equivalent to a mutex, where at most one thread can hold such a lock, the synchronous block of code protected by the lock is atomically executed, and m
Java concurrent programming instance (synchronized) and java concurrent programming instance
Here, we use a small program to illustrate that the logic is a counter (int I). The main logic function is that if resource I is monitored synchronously, no I value is output, however, if the keyword synchronized is not added,
methods, you can use the class's static field class to get the class object. In the above example, the Method1 and Method2 methods can only have one method execution at a time. In addition to using the class field to get the class object, you can also use the GetClass method of the instance to get the class object. The code in the example above can be modified as follows:Using the GetClass method to get th
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
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 Synchronized keyword, the lock of the object
variable to provide a separate copy of the variable, So each thread can change its own copy independently, without affecting the copy of the other thread. This is done in the way of space-time (contrary to synchronized), at the expense of memory, which greatly reduces the performance cost of thread synchronization (such as synchronized) and reduces the complexity of thread concurrency control.Personally th
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.