并发集合(八)使用原子变量
声明:本文是《 Java 7 Concurrency Cookbook 》的第一章, 作者: Javier Fernández González 译者:郑玉婷 校对:方腾飞 在Java 1.5中就引入了原子变量,它提供对单个变量的原子操作。当你在操作一个普通变量时,你在Java实现的每个操作,在程序编译时会
声明:本文是《 Java 7 Concurrency Cookbook 》的第一章, 作者: Javier Fernández González 译者:郑玉婷 校对:方腾飞
在Java 1.5中就引入了原子变量,它提供对单个变量的原子操作。当你在操作一个普通变量时,你在Java实现的每个操作,在程序编译时会被转换成几个机器能读懂的指令。例如,当你分配一个值给变量,在Java你只使用了一个指令,但是当你编译这个程序时,这个指令就被转换成多个JVM 语言指令。这样子的话当你在操作多个线程且共享一个变量时,就会导致数据不一致的错误。
为了避免这样的问题,Java引入了原子变量。当一个线程正在操作一个原子变量时,即使其他线程也想要操作这个变量,类的实现中含有一个检查那步骤操作是否完成的机制。 基本上,操作获取变量的值,改变本地变量值,然后尝试以新值代替旧值。如果旧值还是一样,那么就改变它。如果不一样,方法再次开始操作。这个操作称为 Compare and Set(校对注:简称CAS,比较并交换的意思)。
原子变量不使用任何锁或者其他同步机制来保护它们的值的访问。他们的全部操作都是基于CAS操作。它保证几个线程可以同时操作一个原子对象也不会出现数据不一致的错误,并且它的性能比使用受同步机制保护的正常变量要好。
在这个指南,你将学习怎样使用原子变量实现一个银行账户和2个不同的任务:一个存钱到账户和另一个从账户提取钱。在例子的实现中,你将使用 AtomicLong 类。
准备
指南中的例子是使用Eclipse IDE 来实现的。如果你使用Eclipse 或者其他的IDE,例如NetBeans, 打开并创建一个新的java项目。
怎么做呢…
按照这些步骤来实现下面的例子:
[ code language='java']
//1. 创建一个类,名为 Account,来模拟银行账号。
public class Account {
//2. 声明一个私有 AtomicLong 属性,名为 balance,用来储存账号的余额。
private AtomicLong balance;
//3. 实现类的构造函数,初始化它的属性值。
public Account(){
balance=new AtomicLong();
}
//4. 实现一个方法,名为 getBalance(),用来返回余额属性值。
public long getBalance() {
return balance.get();
}
//5. 实现一个方法,名为 setBalance(),用来设置余额属性值。
public void setBalance(long balance) {
this.balance.set(balance);
}
//6. 实现一个方法,名为 addAmount(),来增加余额属性值。
public void addAmount(long amount) {
this.balance.getAndAdd(amount);
}
//7. 实现一个方法,名为 substractAmount() 来减少余额属性值。
public void subtractAmount(long amount) {
this.balance.getAndAdd(-amount);
}
//8. 创建一个类,名为 并一定实现 Runnable 接口。这个类会模拟公司付款。
public class Company implements Runnable {
//9. 声明一个私有 Account 属性,名为 account。
private Account account;
//10. 实现类的构造函数,初始化它的属性值。
public Company(Account account) {
this.account=account;
}
//11. 实现任务的 run() 方法。 使用 account 的 addAmount()方法来让它的余额做10次的递增,递增额为1000。
@Override
public void run() {
for (int i=0; i
account.addAmount(1000);
}
}
//12. 创建一个类,名为 Bank,并一定实现 Runnable 接口。这个类会模拟从一个账号提款。
public class Bank implements Runnable {
//13. 声明一个私有 Account 属性,名为 account。
private Account account;
//14. 实现类的构造函数,初始化它的属性值。
public Bank(Account account) {
this.account=account;
}
//15. 实现任务的 run() 方法。使用 account 的 subtractAmount() 方法来让它的余额做10次的递减,递减额为1000。
@Override
public void run() {
for (int i=0; i
account.subtractAmount(1000);
}
}
//16. 创建例子的主类通过创建一个类,名为 Main 并添加 main()方法。
public class Main {
public static void main(String[] args) {
//17. 创建一个 Account 对象,设置它的余额为 1000。
Account account=new Account();
account.setBalance(1000);
//18. 创建新的 Company 任务和一个线程运行它。
Company company=new Company(account);
Thread companyThread=new Thread(company);
// 创建一个新的 Bank t任务和一个线程运行它。
Bank bank=new Bank(account);
Thread bankThread=new Thread(bank);
//19. 在操控台写上账号的初始余额。
System.out.printf(“Account : Initial Balance: %d\n”,account. getBalance());
//20. 开始线程。
companyThread.start();
bankThread.start();
//21. 使用 join() 方法等待线程的完结并把账号最终余额写入操控台。
try {
companyThread.join();
bankThread.join();
System.out.printf(“Account : Final Balance: %d\n”,account. getBalance());
} catch (InterruptedException e) {
e.printStackTrace();
}
[/code]
它是怎么工作的...
这个例子的关键是 Account 类。在这个类,我们声明了一个 AtomicLong 属性,名为 balance,用来储存账户余额,然后我们使用 AtomicLong 类提供的方法实现了操作余额的方法。为了实现 getBalance() 方法,返回余额的属性值,你要使用 AtomicLong 类的 get() 方法。为了实现 setBalance() 方法,设立余额值,你要使用 AtomicLong 类的 set() 方法。为了实现 addAmount()方法,为余额值加上收入,你要使用 AtomicLong 类的getAndAdd() 方法,用特定的参数值增加它并返回值。最后,为了实现 subtractAmount() 方法,减少余额值,你也要使用 getAndAdd() 方法。
接着,你实现了2个不同的任务:
Company 类模拟了一个公司,增加余额值。这个类的每次任务会做10次的递增,递增值为1000。
Bank 类模拟了一个银行,银行作为账号的拥有者而收取费用。这个类的每次任务会做10次的递减,递减值为1000。
在 Main 类,你创建了一个有1000余额的 Account 对象。然后,你运行一个银行任务和一个公司任务,所以最终的账号余额一定是等同于初始余额。
当你运行程序,你可以发现你的最终余额和你的初始值一样。以下的截图是例子的运行结果的输出:
更多...
记得我们之前提到的,Java还有其他的原子类哦。例如:AtomicBoolean, AtomicInteger, 和 AtomicReference。
参见
第二章,基本线程同步:同步一个方法
原文地址:并发集合(八)使用原子变量, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

Concurrency and coroutines are used in GoAPI design for: High-performance processing: Processing multiple requests simultaneously to improve performance. Asynchronous processing: Use coroutines to process tasks (such as sending emails) asynchronously, releasing the main thread. Stream processing: Use coroutines to efficiently process data streams (such as database reads).

Concurrency and multithreading techniques using Java functions can improve application performance, including the following steps: Understand concurrency and multithreading concepts. Leverage Java's concurrency and multi-threading libraries such as ExecutorService and Callable. Practice cases such as multi-threaded matrix multiplication to greatly shorten execution time. Enjoy the advantages of increased application response speed and optimized processing efficiency brought by concurrency and multi-threading.

Go language is a simple, efficient, and highly concurrency programming language. It is an open source language developed by Google. In the Go language, the use of spaces is very important, it can improve the readability and maintainability of the code. This article will introduce how to use spaces correctly in Go language and provide specific code examples. Why you need to use spaces correctly In the programming process, the use of spaces is very important for the readability and beauty of the code. Appropriate use of spaces can make code clearer and easier to read, thus reducing

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.
