java 设置原子属性_JAVA存取对象属性时,如果开程多线程,记得对相关存取方法作原子化操作定义...

最显著的应用当然是银行存款和取款,不要存在存取数字和实际发生不一样的情况。

synchronized关键字。

class BankAccount {

private int balance = 100;

public int getBalance() {

return balance;

}

public void withdraw(int amount) {

balance = balance - amount;

}

}

public class RyanAndMonicaJob implements Runnable {

private BankAccount account = new BankAccount();

public static void main(String[] args) {

// TODO Auto-generated method stub

RyanAndMonicaJob theJob = new RyanAndMonicaJob();

Thread one = new Thread(theJob);

Thread two = new Thread(theJob);

one.setName("Ryan");

two.setName("Monica");

one.start();

two.start();

}

public void run() {

for (int x = 0; x < 10; x++) {

makeWithdrawal(10);

if (account.getBalance() < 0) {

System.out.println("Overdrawn!");

}

}

}

private synchronized void makeWithdrawal(int amount) {

if (account.getBalance() >= amount) {

System.out.println(Thread.currentThread().getName() + " is about to withdraw");

try {

System.out.println(Thread.currentThread().getName() + " is going to sleep");

Thread.sleep(500);

} catch (InterruptedException ex) {

ex.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + " woke up.");

account.withdraw(amount);

System.out.println(Thread.currentThread().getName() + " completes the withdrawl");

} else {

System.out.println("Sorry, not enough for " + Thread.currentThread().getName());

}

}

}

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Ryan is about to withdraw

Ryan is going to sleep

Ryan woke up.

Ryan completes the withdrawl

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

Sorry, not enough for Monica

【java】java获取对象属性类型、属性名称、属性值

java获取对象属性类型.属性名称.属性值 获取属性 修饰符:[在Field[]循环中使用] String modifier = Modifier.toString(fields[i].getModi ...

探究&commat;property申明对象属性时copy与strong的区别

一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...

java中对象多态时成员变量,普通成员函数及静态成员函数的调用情况

/* 样例1: class Parent{ int num = 3; } class Child extends Parent{ int num = 4; } */ /* 样例2: class Par ...

Java中对象创建时的内存分配

一.前言知识铺垫   1.逃逸对象:在一个方法内创建的对象没有被外界引用则称该对象为未逃逸的对象. 2.JDK1.6以后的HotSpot虚拟机支持运行时的对象逃逸分析. 3.JVM中的参数配置: 1) ...

java获取对象属性类型、属性名称、属性值

/** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...

java获取对象属性类型、属性名称、属性值 【转】

/** * 根据属性名获取属性值 * */ private Object getFieldValueByName(String fieldName, Object o) { try { String ...

Vue2实践computed监听Vuex中state对象中的对象属性时发生的一些有趣经历

今天想实现一个功能,在全局中随时改变用户的部分信息.这时候就想到了用Vuex状态控制器来存储用户信息,在页面中使用computed来监听用户这个对象.看似一个很简单的逻辑,就体现了我基本功的不扎实呀. ...

java之对象创建时各成员变量的初始值

除了byte short int long float double char bollean这基础类型外,其余的都是引用类型 成员变量类型 初始值 byte 0 short 0 int 0 long ...

随机推荐

IPv6正则表达式

斯蒂芬·瑞恩写了一个非常有用的正则表达式,可用于匹配任何一个合法的IPv6地址.以下为正则表达式的代码: ^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|: ...

&lbrack;CareerCup&rsqb; 9&period;9 Eight Queens 八皇后问题

9.9 Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board so that non ...

亲测linux6&period;4 安装

1.bios下点击 u盘 启动进入(两个Flash1.0,都试试) 2.最关键的部分是,不如windows启动 没有linux界面. others(只是把这个修改一下名字为windows7) cent ...

UserDefault数据读取

//GameScene.h #include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer{pu ...

在VS解决方案资源管理器中自动定位当前编辑中的文件

依次点击 [工具]- [选项] - [项目和解决方案]-[常规]- 勾选[在解决方案资源管理器中跟踪活动项]

一个Python开源项目-腾讯哈勃沙箱源码剖析&lpar;上&rpar;

前言 2019年来了,2020年还会远吗? 请把下一年的年终奖发一下,谢谢... 回顾逝去的2018年,最大的改变是从一名学生变成了一位工作者,不敢说自己多么的职业化,但是正在努力往那个方向走. 以前 ...

Atitit 函数调用的原理与本质attilax总结&&num;160&semi;stdcall&&num;160&semi;cdecl区别

Atitit 函数调用的原理与本质attilax总结 stdcall cdecl区别 通常来说函数调用要用到的两条基本的指令:”CALL”指令和”RET”指令.”CALL”指令将当前的指令指针(这个指 ...

&lbrack;BZOJ4260&rsqb;Codechef REBXOR&lpar;Trie&rpar;

Trie模板题.求出每个前缀和后缀的最大异或和区间,枚举断点就可.不知为何跑得飞快. #include #include #include&l ...

Tkinter Listbox&lpar;列表框&rpar;

Python - Tkinter Listbox(列表框): 列表框部件用于显示一个项目列表,用户可以选择的项目数   列表框部件用于显示一个项目列表,用户可以选择的项目数. 语法: 这里是一个简单的 ...

EJB的优点有哪些?&lpar;选择2项&rpar;

EJB的优点有哪些?(选择2项) A.技术领先 B.价格低廉 C.性能优越 D.强大的容器支持 解答:CD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值