In the previous blog, we explained the Java thread's memory model, see: Java Concurrent programming 2_ thread safety memory model, and then the previous article addressed the issue of thread safety in the case of multi-threaded shared resources.analysis of non-thread threadspublic class Test implements Runnable {private int i = 0;private int GetNext () {return i++;} @Overridepublic void Run () {//Synchroni
For example, an object is like a big house, and the door opens forever. There are a lot of rooms in the house (ie method). These rooms have a locked (synchronized method) and are not locked (common method). A key was placed at the door of the room, and the key opened all the locked rooms. In addition, I compare all the threads that want to call the object method to the person who chengxiang into a room in t
lock and response interrupt lock, which gives us a lot of flexibility. For example: If a, B2 thread to compete lock, a thread get lock, b thread wait, but a thread this time really have too many things to deal with, is not return, B thread may be able to wait, want to interrupt themselves, no longer wait for this lock, to deal with other things. This time Reentrantlock provides 2 mechanisms, first, the B thread interrupts itself (or another thread in
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 () method ( static ) is asynchronous , which illustr
The Java language keyword that, when used to decorate a method or a block of code, guarantees that at most one thread executes the code at the same time.First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it
two methods of sleep time consumption, there is no problem;
The third part is to calculate the i++, then output the result, this part of the output is relatively slow. Because the plus method is a class lock, at the same time only one object owns the lock, so multiple threads must sequentially execute the result, so the output of the last I is also 10.
For object locks, when an object has a lock, it accesses a method with an object lock, and the method calls another method of object locking in
In the case of multithreading, multiple threads of the same process share the same storage space, which brings convenience while also causing access conflicts. Java provides a dedicated mechanism to resolve such conflicts,This effectively prevents the same data object from being simultaneously accessed by multiple threads. Because we can use private keywords to ensure that data objects can only be accessed by methods, we only need to propose a mechani
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 is changed to the
First, the basic use of synchronizedSynchronized is one of the most common methods for solving concurrency problems in Java, and is the simplest one. There are three main functions of synchronized:(1) Ensure thread-mutually exclusive access synchronization code(2) Ensure that changes to shared variables are visible in time(3) Solve the reordering problem effectively. Syntactically speaking, there are three
In concurrent environments, you can consider the use of lock mechanisms when resolving shared resource conflict issues.
1. Lock of Object
All objects automatically contain a single lock.
The JVM is responsible for tracking the number of times an object is locked. If an object is unlocked, its count becomes 0. When a task (thread) locks an object for the first time, the count changes to 1. The count increments whenever this same task (thread) Gets a lock on this object.
Only the task (thread)
0. Overview Synchronized is a built-in lock mechanism provided by Java to enable synchronous access to code blocks, calledbuilt-in lock (intrinsic lock)。 The built-in lock consists of two parts: one is a reference to the object as a lock, and the other is a block of code protected by this lock. It is necessary to understand thatsynchronized locks are references to objects, with only one built-in lock on
comparison of these types of locks:
Lock
Advantages
Disadvantages
Applicable scenarios
Biased lock
Locking and unlocking does not require additional consumption, and the execution of a non-synchronous method is less than the nanosecond-level gap.
If there is a lock contention between threads, additional lock revocation consumption is brought.
Applies to only one thread to access the synchronization block scenario.
Lightweig
Java multi-thread synchronized
First, let's look at the next scenario. A cinema sells tickets for four windows at a time. There are only 100 tickets in total for this movie. Check the actual code.
Package cn.com. thread; public class TestThread {public static void main (String [] args) {SellTicketThread t = new SellTicketThread (); new Thread (t, window 1 ). start (); new Thread (t, window 2 ). start (); n
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
threads enter the lock pool of the object.
Wait pool: Assuming that a thread a invokes the waiting () method of an object, thread a releases the lock on the object (since the wait () method must appear in synchronized so that thread a already has a lock on the object before executing the Wait () method). At the same time, thread A enters the waiting pool of the object. If another thread calls the Notifyall () method of the same object, the thread in
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, such as the lock in the Code. Note that the loc
First, the basic use of synchronized
Synchronized is one of the most common methods for solving concurrency problems in Java, and the simplest one. The main functions of the synchronized are three: (1) To ensure that the Access synchronization code (2) is mutually exclusive, so that the modification of the shared vari
Synchronized is a keyword that is used in Java multithreaded programming to serialize the operations between threads. This approach is similar to using exclusive locks in the database for concurrency control, but the difference is that data objects are locked in the database, while Java is locking the code that will be executed.Here are a few things to keep in mi
Java thread and synchronization (synchronized) Sample Code
Import java. Io .*;Import java. util .*;Import java. Text. simpledateformat;
Public class testthread extends thread{Private Static integer threadcounterlock; // used for synchronization to prevent data from being wri
Good memory is better than bad writing ~ ~并发编程中synchronized关键字的地位很重要,很多人都称它为重量级锁。利用synchronized实现同步的基础:Java中每一个对象都可以作为锁。具体表现为以下三种形式。(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 method block, the lock is an obje
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.