"Original" https://www.toutiao.com/i6566022142666736131/When we use threads to create a thread, it's easy to implement, but there's a problem:If the number of concurrent threads is large, and each thread is executing a short task, then the frequent
Wait and notifyWait and notify can implement communication between threads, and when one thread executes a condition that does not meet the criteria, it can call the wait method to put the thread into a waiting state, and when another thread
Synchronous
In multithreaded programs, synchronization modifiers are used to control access to critical section code. One way is to use the Synchronized keyword to ensure thread safety for your code. In Java, synchronized-modified code blocks
Reading notes: C-depth anatomy of the "C language in Depth" this book is a good book to solve the programmer's secret of the interview test. The author Chengzhong "to the gold to brave the challenge of similar books at home and abroad," Indeed, the
Idle Nothing, wrote a producer consumer mode to play, by the way familiar with the use of notify.Package Sync;public class Test {public static void main (string[] args) {test test = new test (); Producer Producer = test.new Producer ();p
In the [high Concurrency Java II] Multithreaded Foundation, we have initially mentioned basic thread synchronization operations. This time, we will mention the synchronization control tool in the contract.
1. Use of various synchronization control
Concurrenthashmap Concurrent ContainerConcurrenthashmap can read data without locking, and its internal structure allows it in the write operation can keep the size of the lock as small as possible, do not have to lock the entire concurrenthashmap.
Why to Thread Sync
Because when we have more than one thread to access a variable or object at the same time, if the threads have both read and write operations, the value of the variable or the state of the object is confused, resulting in an
Exception
57th: Use exceptions only for exceptions
Exceptions should be used only in the case of exceptions: they should never be used for normal control flow
A well-designed API should not force its clients to use exception 58th for normal flow
Synchronized Favorites Summary In the CAS principle Java SE1.6: Synchronized is always the oldest role in multithreaded concurrent programming, and many people call it a heavyweight lock, but as Java SE1.6 has optimized synchronized, and in some
volatile VariableThe volatile variable indicates that it must be consistent with the main memory, which is actually a "synchronization of the variables", meaning that the operation of the volatile variable is atomic, such as before a long or double
Volatile is a variable modifier, and synchronized is a modifier of a method or block. So we use both of these keywords to specify three simple ways to access variables.
int i1; int Geti1 () {return i1;}
volatile int i2; int Geti2 () {return i2;}
int
One, as a switch
A. Atomicboolean
Private Atomicboolean update = new Atomicboolean (false);
public void init ()
{
if (This.update.compareAndSet (False, True))
{
try{
//Do some thing
}
finally{
This.refresh.set (false)
Volatile is used in C # to control the synchronization of the keyword, its meaning is for some sensitive data in the program, do not allow multithreading simultaneous access to ensure that the data at any access time, a maximum of one thread access
Technology News
November 16 Evening News, Ctrip CEO Sun Jie on the parent-child Park event related progress to do final bulletin, Ctrip former vice president of human Resources Shi, the current vice president of human Resources Feng Weihua has been
"Java Source analysis": Concurrenthashmap JDK1.8
Recently has been looking at the source of the j.u.c, understanding the atomic operation, understanding the lock mechanism, understanding multithreading concurrency and so on. But Concurrenthashmap
Basic Knowledge:
Visibility:
Visibility is a complex attribute, because errors in visibility always violate our intuition. In general, we cannot be sure that the thread that performs the read operation can see the value written by other threads in
1.register: Request the compiler to store variables as much as possible in the CPU's registers.
The Register keyword can save the time that the CPU presents data from memory and improve the execution efficiency of the program.
But since the
Objective
Locks are used to control how multiple threads access a shared resource. Before the lock interface appears, the Java program relies on the Synchronized keyword to implement the lock function, although lock is more explicitly acquired and
Read the java.util.concurrent.atomic bag inside each atomicxxx class implementation of students should have seen lazyset methods, such as Atomicboolean class Lazyset method
Public final void Lazyset (Boolean newvalue) {
int v = newvalue 1:0;
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.