In concurrent environments, you can consider the use of lock mechanisms when resolving shared resource conflict issues. 1. Object Lock
All objects automatically contain a single lock.
The JVM is responsible for tracking the number of times an object
Understand the synchronized keyword in java:--------------------------------------------------------------
In fact, more than 90% of my basic java knowledge comes from Thinking in Java. For the synchronized keyword,At that time, I just browsed it. I
A keyword in the Java language that, when used to modify a method or a block of code, ensures that at most one thread at the same time executes that segment of code.
First, when two concurrent threads access the synchronized (this) synchronized
The understanding of synchronize keywords in Java is applied in multithreaded environment:
Synchronized keyword, which includes two usages: Synchronized method and synchronized block.
1. Synchronized method: Declare the Synchronized method by adding
Copy Code code as follows:
Public synchronized void Run ()
{
}
As you can see from the above code, as long as you add the synchronized keyword between void and public, you can synchronize the Run method, that is, for an object
0. About thread synchronization(1) Why do I need to sync multithreading?thread synchronization is to allow multiple running threads to work together in a good way to allow multithreading to properly occupy released resources as required. We use the
The simplest approach is to use the Synchronized keyword to synchronize the Run method, and see the following code as long as the Synchronized keyword is added between void and publicCopy CodeThe code is as follows:Public synchronized void Run
A. When will there be thread safety issues?
Thread-safe issues do not occur in a single thread. In multithreaded programming, it is possible to have simultaneous access to the same resource, which can be a variety of resources: A variable, an
From http://tutorials.jenkov.com/java-concurrency/synchronized.html
by Jakob Jenkov
Table of Contents
The Java synchronized Keyword
Synchronized Instance Methods
Synchronized Static Methods
Synchronized Blocks in
To solve the "dirty data" problem, the simplest method is to use the synchronized keyword to synchronize the run method. The Code is as follows:
Public synchronized void run (){}
From the code above, we can see that as long as the 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.