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 ...
随机推荐
- Android 颜色渲染(一) 颜色选择器 ColorPickerDialog剖析
版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色选择器之ColorPickerDialog剖析 有这样一个需求,可以让用户自定义背景颜色,这就需要提供一个颜色选择器给用户. ...
- Unix网络编程代码 第13章 守护进程和inetd超级服务器
1. 概述 守护进程是在后台运行且不与任何控制终端关联的进程.unix系统通常有很多守护进程在后台运行,执行不同的管理任务. 守护进程没有控制终端通常源于它们由系统初始化脚本启动.然而守护进程也 ...
- ASP.NET Core和ASP.NET Framework共享Identity身份验证
.NET Core 已经热了好一阵子,1.1版本发布后其可用性也越来越高,开源.组件化.跨平台.性能优秀.社区活跃等等标签再加上"微软爸爸"主推和大力支持,尽管现阶段对比.net ...
- [转] linux下查看文件编码及修改编码
如果无法识别文件编码,可能是文件中已有乱码,此时需要去掉乱码 查看文件编码 在Linux中查看文件编码可以通过以下几种方式: 1.在Vim中可以直接查看文件编码 :set fileencoding 即 ...
- 关于css中伪类及伪元素的总结
css中的伪类和伪元素总是混淆,今天参考了很多资料,也查看了部分文档,现将伪类及伪元素总结如下: 一.由来: 伪类和伪元素的引入都是因为在文档树里有些信息无法被充分描述,比如CSS没有"段落 ...
- 开始android博客分享
现在开始写博客,分享android开发中的心得.
- 第三篇:python基础之编码问题
python基础之编码问题 python基础之编码问题 本节内容 字符串编码问题由来 字符串编码解决方案 1.字符串编码问题由来 由于字符串编码是从ascii--->unicode---&g ...
- rabbitmq pika connection closed
You are here: Home / rabbitmq pika connection closed rabbitmq pika connection closed By lijiejie on ...
- PHP 解决未定义变量报错
在PHP中 有时候会出现 Notice: Undefined index: sid in D:\Apache Group\Apache2\htdocs\php_mobile\mobile\chao\s ...
- angularJS随笔
1.作用域 基于作用域的事件传播 作用域可以像DOM节点一样,进行事件的传播.主要是有两个方法: broadcasted :从父级作用域广播至子级 scope emitted :从子级作用域往上发射到 ...