Java Multithreading Basics Summary five: Atomic

Source: Internet
Author: User
Tags cas sleep volatile advantage

Before introducing the Java.util.concurrent.atomic package briefly, there is a concept to be copied and familiar: CAS (compare and Exchange). Most processors now provide support for concurrent access, which is reflected by providing hardware directives to support the special needs of multiprocessing. For example, to detect or block concurrent access to other processors to update the instructions for shared variables. For the Intel x86 architecture processor, it is by providing a hardware primitive instruction set that implements CAS or comparisons. Three operands of CAS operation: memory location (V), expected original value (A) and new value (B). The procedure is usually to predict that the memory address V should contain value A and, if it is included, replace the value B to position V, otherwise, do not change any value to tell the current value of the address v. The CAS approach to read-modify-write is to detect if there are other threads in the process that are modifying variables, and if so this time the CAS operation fails, you can try to restart the CAs. Speaking of this seems to feel more complex than synchronized, whether it means that the cost is not small? The answer is yes. Because it is the hardware of the original implementation of a very lightweight lock-free synchronization. Like High-definition decoding, GPU native hardware decoding than soft solution CPU occupancy advantage that is quite different ah!

When it comes to hardware, I think of the recent crazy argument about the advantage of using a 64-bit operating system. Now the majority of processors support 64 bits, meaning that the processor's register mapping to the memory of the address space is greatly larger, the operating system 64-bit architecture may be in memory management challenges greater, no good memory compression technology, large memory can only be a big waste. It also indicates that if the three-party software developers are unfamiliar with the 64-bit system memory management, the probability of the software becoming garbage becomes larger. Without the prosperity of the 64-bit tripartite software, what can the operating system do only as a platform to support software operations? So the advantage is not in the operating system itself but in the platform software. Again pull far, eh ...

The JDK5 later provided more than 10 atomic classes under the Java.util.concurrent.atomic package. The common atomicinteger,atomiclong,atomicreference and their array forms, as well as the Atomicboolean and the Atomicstampedreference classes introduced to address the ABA problem, Finally, a utility class that is based on reflection for updating volatile variables: Atomicintegerfieldupdater,atomiclongfieldupdater,atomicreferencefieldupdater. These atomic classes can theoretically improve performance significantly. And within the java.util.concurrent of concurrent sets, thread pools, actuators, Synchronizer, the internal implementation of a large number of rely on these unlocked atomic classes, thus striving for maximum performance. Here's a simple example to see:

Java code

Import Java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
Import Java.util.concurrent.atomic.AtomicInteger;

/**
* User:yanxuxin
* Date:dec 16, 2009
* Time:10:49:40 PM
*/
public class Atomiccountersample extends Thread {
Private Atomiccounter Atomiccounter;

Public Atomiccountersample (Atomiccounter atomiccounter) {
This.atomiccounter = Atomiccounter;
}

@Override
public void Run () {
Long Sleeptime = (long) (Math.random () * 100);
try {
Thread.Sleep (Sleeptime);
catch (Interruptedexception e) {
E.printstacktrace ();
}

Atomiccounter.counterincrement ();
}

public static void Main (string[] args) throws Exception {
Atomiccounter atomiccounter = new Atomiccounter ();

for (int i = 0; i < 5000; i++) {
New Atomiccountersample (Atomiccounter). Start ();
}

Thread.Sleep (3000);

System.out.println ("counter=" + atomiccounter.getcounter ());
}
}

Class Atomiccounter {
Private Atomicinteger counter = new Atomicinteger (0);

public int Getcounter () {
return Counter.get ();
}

public void Counterincrement () {
for (;;) {
int current = Counter.get ();
int next = current + 1;
if (Counter.compareandset (current, next))
Return
}
}
}

Class AtomicCounter2 {
private volatile int counter;
private static final atomicintegerfieldupdater<atomiccounter2> Counterupdater = Atomicintegerfieldupdater.newupdater (Atomiccounter2.class, "counter");

public int Getcounter () {
return counter;
}

public int counterincrement () {
return counter++;
Return counterupdater.getandincrement (this);
}
}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.