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获取对象属性类型、属性名称、属性值
java获取对象属性类型.属性名称.属性值 获取属性 修饰符:[在Field[]循环中使用] String modifier = Modifier.toString(fields[i].getModi ...
- 探究@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中对象属性可以是另外一个对象或对象的参考
7.对象的属性可以是另外一个对象或对象的参考 通过这种方法可以迅速构建一个比较大的系统. class Motor { Light[] lights; Handle left, ri ...
- 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来监听用户这个对象.看似一个很简单的逻辑,就体现了我基本功的不扎实呀. ...
- [改错_19/04/01] 学习Java.IO 对象数据流时出现 Exception in thread "main" java.io.EOFException ...at cn.sxt.test.Test_DataStream.main(Test_DataStream.java:31) 错误 .
过程描述:编译可以通过,就是每次运行时出现如下的图片,百思不得其解. 错误原因: byte[] datas=baos.toByteArray(); 放在了oos.writeInt(14);oos.fl ...
随机推荐
- [React] Intro to inline styles in React components
React lets you use "inline styles" to style your components; inline styles in React are ju ...
- max_connections 与 max_used_connections --ERROR 1040: Too many connections
mysql> show variables like 'max_connections'; +-----------------+-------+ | Variable_name | Value ...
- Hadoop WritableComparable接口
WritableComparable接口 Writable接口大家可能都知道,它是一个实现了序列化协议的序列化对象.在Hadoop中定义一个结构化对象都要实现Writable接口,使得该结构化对象可以 ...
- 怎样写好一份IT技术岗位的简历
10月是校园招聘的旺季,很多应届毕业生都忙碌起来了,从CSDN笔试-面试文章的火热程度,从我收到的简历就看得出来. 我很久没有参与笔试和面试了,所以只能从“简历”来阐述下我的看法. 截至目前,已经帮8 ...
- Maven Integration for Eclipse 正确地址
m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...
- svn设置
svnserver -d -r /home/peter.mycode 如果想要开机自启动,将上述启动命令添加到:/etc/rc.local中.
- angularjs hover
<ul class="pdl-15"><li ng-repeat="order in vm.selectOrders" ng-class=&q ...
- 一则自用iptables例子解释
公网IP:110.24.3.83内网IP:10.252.214.186局域网数据库:10.252.214.100 通过NAT端口转发,访问110.24.3.83:3308端口跳转到局域网数据库机器的3 ...
- 分享最近和同事处理的 解析XML的相关问题
CREATE OR REPLACE PROCEDURE BATCHINSERTSK_DEVICE_RECORD1( xmlstr IN clob, v_commits o ...
- os项目icon和default 等相关图标命名规则和大小设置
最新的参考apple官网地址:https://developer.apple.com/library/ios/qa/qa1686/_index.html,网页下面有详细的使用方法(ios7以后的) 转 ...