Java Base thread Atomic weight

Source: Internet
Author: User

The so-called atomic weight is the operation of the variable operation is "atomic", the operation is not re-divided, it is thread-safe. Why use atomic variables, because multiple threads can also cause problems with a single variable operation. The security of concurrent access can be addressed through the volatile, synchronized keyword before Java5, but it is too cumbersome. After Java5, a toolkit java.util.concurrent.atomic for single-variable multithreaded concurrency security access is provided, and the classes are simple

 PackageUnit_fifteen;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.atomic.AtomicLong;/*** Java Threads: New Features-Atomic weight **/  Public classTest { Public Static voidMain (string[] args) {Executorservice pool= Executors.newfixedthreadpool (2); Runnable T1=NewMyrunnable ("Zhang San", 2000); Runnable T2=NewMyrunnable ("John Doe", 3600); Runnable T3=NewMyrunnable ("Harry", 2700); Runnable T4=NewMyrunnable ("Lao Zhang", 600); Runnable T5=NewMyrunnable ("Lao Niu", 1300); Runnable T6=NewMyrunnable ("Fat", 800); //Executing individual threadsPool.execute (t1);                 Pool.execute (T2);                 Pool.execute (T3);                 Pool.execute (T4);                 Pool.execute (T5);                 Pool.execute (T6); //Close the thread poolPool.shutdown (); } } classMyrunnableImplementsRunnable {Private StaticAtomiclong along =NewAtomiclong (10000);//atomic weight, each thread is free to operate        PrivateString name;//Operating Person        Private intX//Operating Amountmyrunnable (String name,intx) { This. Name =name;  This. x =x; }          Public voidrun () {System.out.println (name+ "performed" + x + ", Current balance:" +along.addandget (x)); } }

Above is a counter example, the result of the code is changeable

Note: Although the atomic weight can guarantee the safety of a single variable during an operation, it cannot guarantee the security of your entire block of code, or the entire program. Therefore, synchronization mechanisms such as locks should often be used to control the security of the entire program.

 PackageUnit_fifteen;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJava.util.concurrent.locks.Lock;ImportJava.util.concurrent.locks.ReentrantLock;ImportJava.util.concurrent.atomic.AtomicLong;/*** Java Threads: New Features-Atomic weight **/  Public classTest { Public Static voidMain (string[] args) {Executorservice pool= Executors.newfixedthreadpool (2); Lock Lock=NewReentrantlock (false); Runnable T1=NewMyrunnable ("Zhang San", 2000, lock); Runnable T2=NewMyrunnable ("John Doe", 3600, lock); Runnable T3=NewMyrunnable ("Harry", 2700, lock); Runnable T4=NewMyrunnable ("Lao Zhang", 600, lock); Runnable T5=NewMyrunnable ("Lao Niu", 1300, lock); Runnable T6=NewMyrunnable ("Fat", 800, lock); //Executing individual threadsPool.execute (t1);                 Pool.execute (T2);                 Pool.execute (T3);                 Pool.execute (T4);                 Pool.execute (T5);                 Pool.execute (T6); //Close the thread poolPool.shutdown (); } } classMyrunnableImplementsRunnable {Private StaticAtomiclong along =NewAtomiclong (10000);//atomic weight, each thread is free to operate        PrivateString name;//Operating Person        Private intX//Operating Amount        Privatelock lock; Myrunnable (String name,intX,lock Lock) {                 This. Name =name;  This. x =x;  This. Lock =Block; }          Public voidrun () {lock.lock (); SYSTEM.OUT.PRINTLN (Name+ "performed" + x + ", Current balance:" +along.addandget (x));         Lock.unlock (); } }

Java Base thread Atomic weight

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.