当前位置: 首页 > news >正文

Java同步问题_Java中的同步问题

展开查看详情

1.Synchronization 1

2.Overview synchronization mechanisms in modern RTEs concurrency issues places where synchronization is needed structural ways (design patterns) for exclusive access 2

3.Overview synchronization mechanism in Java RTE Visibility and re-ordering Design patterns to avoid safety issues: monitor pattern. client side locking. class composition. version iterattor . 3

4.Java Synchronization - concurrent objects safety threads which access the same object concurrently. safety - synchronize access to internal state of the object 4

5.class Even 5

6.if threads can access internal state of object concurrently - not safe @invariant is not guaranteed code perfectly safe in sequential environment 6

7.Visibility and Reordering – unsafe constructs in concurrent execution Visibility - when threads see values written to memory by other threads Reordering - compiler / RTE change code to optimize it 7

8.8

9.anonymous class - class that has no name combines class declaration and instance creation in one step in-line allows "on the fly" creation of object reduce the number of .java files necessary class used in specific situation: Comparator compiler will create separate class files: ClassName$SomeNumber.class 9

10.anonymous class - class that has no name Rules: always extend a super class or implement an interface implement all abstract methods in super class or interface. uses default constructor from the super class to create an instance 10

11.Inlining 11

12.Inlining Inline expansion - compiler optimization that replaces a function call site with the body of the callee . improve time and space usage at runtime, at cost of binary file size 12

13.13

14.output = 0 thread t does not "see" the change to the state of d performed in main thread thread main - implicitly created by JVM executes main() method main calls  d.set (10): 10 is written to memory:  i _ member of: VisibilityDemo  object t reads from i _ ( d.get () ) Java does not guarantee t sees most recent value written to this memory location 14

15.Re-ordering compiler must be able to re-order simple instructions to optimize our code 15

16.Re-ordering compiler guarantees safe reordering in non-concurrent execution environments. order of operations is that the sequence will end with operation 4 16

17.Re-ordering in concurrent environments, reordering may harm 17

18.“must-happen-before” compiler would not reorder if "must-happen-before" constraint 18

19.“must-happen-before” "must-happen-before“ - call to any (non-inline) function compiler would not go recursively into that function 19

20.“must-happen-before” "must-happen-before“ - call to any (non-inline) function compiler would not go recursively into that function 19

21.synchronized Java coordinates actions of threads on object: keyword  before method name in class definition: public synchronized int add() - only one thread is allowed to enter constructors cannot be synchronized - syntax error ( only thread that creates an object access its constructor ) 21

22.synchronized Java RTE ensures that only one thread access this method. solves visibility and reordering problems; " write " to memory during synchronized code section is guaranteed to be returned to all " read " operations following it 22

23.How "synchronized" is implemented? Java mechanism - use it better / avoid common pitfalls each object inside JVM (at runtime) has a lock ("monitor") JVM maintain/initialize locks. cannot access locks explicitly access implicitly using synchronized   23

24.locks locks are in one of two states; in possession of thread available thread T tries to acquire the lock of any object. if available – lock transferred to T possession otherwise, T sleeps ( queue) until lock available T wakes up and tries to acquire lock 24

25.synchronized each thread, calling the method, must first acquire lock. when thread exits – thread releases lock 25

26.The cost of locks – time, concurrency memory sync.: after thread exits – memory of all threads synchronize with main memory   thread may cache copies of memory in its own memory (e.g., CPU registers / CPU cache) blocking: threads must wait for each other 26

27.When to Use Synchronization mutable variable accessible from different threads must be protected with lock else, you may be hit by re-ordering problems monitor pattern - internal state of object is encapsulated . objects are responsible to synchronize access to state: synchronized get() /set()   27

28.When to Use Synchronization invariant involving several members - synchronized with same lock. never lock large parts of code. minimize code sections which use locking 28

29.Synchronized Properties synchronized keyword is not part of the method signature. synchronized keyword cannot be specified in an interface. constructors cannot be  synchronized no point - as long as constructor is not complete, object instance is not accessible to other threads, unless… static method as synchronized - lock associated with the class to be used (each class in Java is also an object on its own) Java locks are Reentrant : same thread holding the lock can get it again and again. 29

相关文章:

  • ubuntu php 编译安装mysql_ubuntu 8.04 编译安装 php mysql apache
  • java 构造器 this_Java 类的构造器中this()和super()的困惑
  • java为什么输入else错误_java – 我遇到了一个错误问题,我正在使用“else if”语句....
  • java antlr sql_ANTLR SQL解析器代码
  • java cms gc_G1,CMS及PARALLEL GC的比较
  • java以class开头_JAVA基础:从.class文件中寻找类名
  • Java 习题6 参考答案及解析_java期末复习题答案及解析
  • 猜字小游戏java方法体_java实现猜字小游戏
  • java游戏开发包_LWJGL
  • java的人patch方法_java – 如何在CXF中使用PATCH方法
  • java func_Java通过匿名类来实现回调函数实例总结
  • Java关系表达式x y_Java正则逻辑运算符[X|Y]匹配
  • java将五元换成1元5角_C,一个代码来获得一笔钱转换成四分之一,硬币,镍币,便士[关闭]...
  • java 对方 GBK 乱码_【转】Java编码与乱码---GBK与UTF-8之间的转换
  • mySQL表变量和临时表的区别_SQL Server表变量和临时表的区别
  • 【Linux系统编程】快速查找errno错误码信息
  • Android交互
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • Javascript弹出层-初探
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • log4j2输出到kafka
  • opencv python Meanshift 和 Camshift
  • session共享问题解决方案
  • ubuntu 下nginx安装 并支持https协议
  • 记一次删除Git记录中的大文件的过程
  • 今年的LC3大会没了?
  • 聊一聊前端的监控
  • 前端
  • 三栏布局总结
  • 深度学习中的信息论知识详解
  • 数据结构java版之冒泡排序及优化
  • 小程序测试方案初探
  •  一套莫尔斯电报听写、翻译系统
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • ​TypeScript都不会用,也敢说会前端?
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • (1)Nginx简介和安装教程
  • (安全基本功)磁盘MBR,分区表,活动分区,引导扇区。。。详解与区别
  • (规划)24届春招和25届暑假实习路线准备规划
  • (六)Hibernate的二级缓存
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (七)Knockout 创建自定义绑定
  • (三)uboot源码分析
  • (十八)SpringBoot之发送QQ邮件
  • (十三)Maven插件解析运行机制
  • (循环依赖问题)学习spring的第九天
  • (转) Android中ViewStub组件使用
  • . Flume面试题
  • .bashrc在哪里,alias妙用
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .Net 路由处理厉害了
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args