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
One, synchronized synchronous statement block:
Before introducing the synchronization statement block, do an experiment to verify that the order in which multiple threads call the same synchronization method is random.
Example one:
1) Mylist.java
2)
For example, an object is like a big house, and the door is always open. There are many rooms in the house (that is, the method ). These rooms are locked (Synchronized Method) and cannot matchLocks (common method ). There is a key at the door of the
The explanation in the book is very simple and clear, but it may be a little difficult to use.
The synchronization object of synchronized method refers to the class instance object,
The synchronization object of synchronized static method refers to
During thread synchronization, synchronized is usually used to modify the method. Many people think that this lock is only used to lock the method, but its essence is to lock the object (such as object );
When a thread calls a synchronized modified
Thinking...
Reference: http://hanhongke123.blog.163.com/blog/static/62223494201081392124586/
No. One Synchronized Method of an object can only be accessed by one thread.
Java synchronized usage Summary
1. When synchronized is used as a function
In multi-threaded development, there are always multiple ways to manipulate the same data in different threads so that the data is manipulated in different order in different threads.Or timing can lead to a variety of different phenomena that do not
In two different situations
1): Non-Synchronized method to enter this object
Answer: You can
2): The synchronization method to enter this object
Answer: No, you can't.
The first case of the original code
/**
* * *
/package thread;
/**
* @author
The Synchronized method of an object cannot be entered, but its other synchronized methods are accessible.
Let's take a look at an example:
Package dhp.test1;
public class TT implements Runnable {
int b = m;
Public synchronized void M1
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
1, multithreading synchronization:1.1, synchronization mechanism:in multiple threads, there may be multiple threads trying to access a limited resource that must be prevented from happening. So the synchronization mechanism is introduced: when a
Synchronized keyword
Synchronized, we call the lock, mainly used to lock the method, code block. When a method or block of code uses synchronized, at most one thread at the same time executes the code. When more than one thread accesses the lock
The Synchronized keyword, which represents a lock on this method, is equivalent to whichever thread (such as thread A) is running to this method, checking that there are other threads B (or C, D, etc.) using this method (or other synchronization
Notes
13: Concurrent Programming
Object-oriented enables us to divide the program into separate modules. But you will often encounter, not only to break down the program, but also to make its various parts can run independently of the
Original link:Http://www.cnblogs.com/dolphin0520/p/3923737.htmlAlthough multi-threading programming greatly improves efficiency, it also poses some pitfalls. For example, two threads that insert non-duplicated data into a database table can cause
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
Android System ANR caused SWT restart issue, androidanrI. symptom
1. What users can see is System first ANR.
2. After ANR, the system restarts.
Test method:
On the recording page, the volume progress bar is continuously swiped. At the same time,
Java Concurrency Programming: synchronizedAlthough multi-threading programming greatly improves efficiency, it also poses some pitfalls. For example, two threads that insert non-duplicated data into a database table can cause the same data to be
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.