In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use AtomicIntegerFieldUpdater". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use AtomicIntegerFieldUpdater.
AtomicXXXFieldUpdater is some fields that can be manipulated with non-thread-safe objects in a thread-safe manner. For example: 1000 people transfer one yuan to an account at the same time, then the cumulative increase should be 1000 yuan, in addition to lock and AtomicInteger can also be realized using FieldUpdater. Account class-BackAccountimport java.util.concurrent.atomic.AtomicIntegerFieldUpdater;public class BackAccount {private volatile int money; private static final AtomicIntegerFieldUpdater updater = AtomicIntegerFieldUpdater.newUpdater (BackAccount.class, "money"); BackAccount (int money) {this.money = money;} public void incMoney () {/ / manipulate fields updater.incrementAndGet (this) through AtomicIntegerFieldUpdater;} public int getMoney () {return money }} Task class-Taskpublic class Task implements Runnable {private BackAccount backAccount; Task (BackAccount account) {this.backAccount = account;} @ Override public void run () {backAccount.incMoney ();}} launch class import java.util.ArrayList;import java.util.List;public class Main {public static void main (String [] args) throws InterruptedException {BackAccount account = new BackAccount (0); List list = new ArrayList () For (int I = 0; I
< 1000 ; i++) { Thread t = new Thread(new Task(account)); list.add(t); t.start(); } for(Thread t : list) { t.join(); } System.out.println(account.getMoney()); }}运行结果At this point, I believe you have a deeper understanding of "how to use AtomicIntegerFieldUpdater". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.