Java Threads: New features-Atomic weights

Source: Internet
Author: User

The so-called atomic weight is the operation of the variable is "atom", the operation can not be divided, so it is thread-safe.

Why use atomic variables because multiple threads can also cause problems with a single variable operation. Before Java5, you can solve the security problems of concurrent access by volatile, synchronized keywords, but this is too cumbersome.

JAVA5 provides a toolkit java.util.concurrent.atomic for single variable multithreaded concurrency security access, and the classes are simple.

Here is a negative example (do not imitate):

Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.atomic.AtomicLong;
/**
* Java Thread: New feature-Atomic weight
*
* @author Leizhimin
*/
public class Test {
public static void Main (string[] args) {
Executorservice pool = Executors.newfixedthreadpool (2);
Runnable T1 = new Myrunnable ("John", 2000);
Runnable t2 = new Myrunnable ("Dick", 3600);
Runnable t3 = new Myrunnable ("Harry", 2700);
Runnable T4 = new Myrunnable ("Old Zhang", 600);
Runnable T5 = new myrunnable ("Old Ox", 1300);
Runnable T6 = new Myrunnable ("Fatso", 800);
Executing individual threads
Pool.execute (t1);
Pool.execute (T2);
Pool.execute (T3);
Pool.execute (T4);
Pool.execute (T5);
Pool.execute (T6);
Close the thread pool
Pool.shutdown ();
}
}
Class Myrunnable implements Runnable {
private static Atomiclong along = new Atomiclong (10000); Atomic weights, each thread is free to operate
private String name; Operation Person
private int x; Operating Amount
Myrunnable (String name, int x) {
THIS.name = name;
this.x = x;
}
public void Run () {
SYSTEM.OUT.PRINTLN (name + "executes + x +", Current balance: "+ along.addandget (x));"
}
}

Run Result:

李四执行了3600,当前余额:13600
王五执行了2700,当前余额:16300
老张执行了600,当前余额:16900
老牛执行了1300,当前余额:18200
胖子执行了800,当前余额:19000
张三执行了2000,当前余额:21000
Process finished with exit code 0

张三执行了2000,当前余额:12000
王五执行了2700,当前余额:18300
老张执行了600,当前余额:18900
老牛执行了1300,当前余额:20200
胖子执行了800,当前余额:21000
李四执行了3600,当前余额:15600
Process finished with exit code 0

张三执行了2000,当前余额:12000
李四执行了3600,当前余额:15600
老张执行了600,当前余额:18900
老牛执行了1300,当前余额:20200
胖子执行了800,当前余额:21000
王五执行了2700,当前余额:18300
Process finished with exit code 0

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.