Safe Concurrent Programming in Java
更新时间:2023-07-25 20:59:01 阅读量: 实用文档 文档下载
- safe推荐度:
- 相关推荐
Multithreaded programming is difficult and error prone. Multithreaded programs typically synchronize operations on shared mutable data to ensure that the operations execute
Chandrasekhar Boyapati CHANDRA@LCS.MIT.EDU Robert Lee RHLEE@LCS.MIT.EDU Martin Rinard RINARD@LCS.MIT.EDU MIT Laboratory for Computer Science,200Technology Square,Cambridge MA,02139USA
1.Introduction
Multithreaded programming is dif?cult and error prone. Multithreaded programs typically synchronize operations on shared mutable data to ensure that the operations exe-cute atomically.Failure to correctly synchronize such op-erations can lead to data races or deadlocks.A data race occurs when two threads concurrently access the same data without synchronization,and at least one of the accesses is a write.A deadlock occurs when there is a cycle of the form:,Thread holds Lock and Thread is waiting for Lock.
This paper presents a new static type system for multi-threaded programs;well-typed programs in our system are guaranteed to be free of data races and deadlocks.In recent previous work,we presented a static type system to prevent data races.In this paper,we extend the race-free type sys-tem to prevent both data races and deadlocks.The basic idea behind our system is as follows.When programmers write multithreaded programs,they already have a lock-ing discipline in mind.Our system allows programmers to specify this locking discipline in their programs.The resulting speci?cations take the form of type declarations. To prevent data races,programmers associate every object with a protection mechanism that ensures that accesses to the object never create data races.The protection mecha-nism of an object can specify either the mutual exclusion lock that protects the object from unsynchronized concur-rent accesses,or that threads can safely access the object without synchronization because either1)the object is im-mutable,2)the object is accessible to a single thread,or 3)the variable contains the unique pointer to the object. Unique pointers are useful to support object migration be-tween threads.The type checker statically veri?es that a program uses objects only in accordance with their de-clared protection mechanisms.
To prevent deadlocks,programmers partition all the locks into a?xed number of lock levels and specify a partial order among the lock levels.The type checker statically veri?es that whenever a thread holds more than one lock,the thread acquires the locks in the descending order.Our system also allows programmers to use recursive tree-based data struc-tures to further order the locks that belong to the same lock level.For example,programmers can specify that nodes in a tree must be locked in the tree-order.Our system allows mutations to the data structure that change the partial order at runtime.The type checker uses an intra-procedural intra-loop?ow-sensitive analysis to statically verify that the mu-tations do not introduce cycles in the partial order,and that the changing of the partial order does not lead to deadlocks. Although our type system is explicitly typed in principle,it would be onerous to fully annotate every method with the extra type information that our system requires.Instead, we use a combination of intra-procedural type inference and well-chosen defaults to signi?cantly reduce the num-ber of annotations needed in practice.Our approach per-mits separate compilation.We have a prototype implemen-tation of our type system that handles all the features of the Java language.We also modi?ed several multithreaded Java programs and implemented them in our system.These programs exhibit a variety of sharing patterns.Our expe-rience shows that our system is suf?ciently expressive and requires little programming overhead.
2.The Type System
This section introduces our type system with two examples. Figure1presents an example program that has an Account class and a CombinedAccount class.To prevent data races,programmers associate every object with a protec-tion mechanism.In the example,the CombinedAccount class is declared to be immutable.A CombinedAccount may not be modi?ed after initialization.The Account class is generic—different Account objects may have dif-ferent protection mechanisms.The CombinedAccount class contains two Account?elds—savingsAccount and checkingAccount.The key word self indicates that these two Account objects are protected by their own locks.The type checker statically ensures that a thread holds the locks on these Account objects before accessing the Account objects.
Multithreaded programming is difficult and error prone. Multithreaded programs typically synchronize operations on shared mutable data to ensure that the operations execute
1class Account {2int balance =0;
34int balance()accesses (this){return balance;}5void deposit(int x)accesses (this){balance +=x;}6void withdraw(int x)accesses (this){balance -=x;}7}89class CombinedAccount<readonly>{10LockLevel savingsLevel =new;11LockLevel checkingLevel <savingsLevel;12final Account<self:savingsLevel>savingsAccount 13=new Account;14final Account<self:checkingLevel>checkingAccount 15=new Account;1617void transfer(int x)locks(savingsLevel){18synchronized (savingsAccount){19synchronized (checkingAccount){20savingsAccount.withdraw(x);21checkingAccount.deposit(x);22}}}23int creditCheck()locks(savingsLevel){24synchronized (savingsAccount){25synchronized (checkingAccount){26return savingsAccount.balance()+27checkingAccount.balance();28}}}29 (30)
}Figure bined Account Example To prevent deadlocks,programmers associate every lock in our system with a lock level.In the example,the CombinedAccount class declares two lock levels—savingsLevel and checkingLevel .Lock levels are purely compile-time entities—they are not preserved at runtime.In the example,checkingLevel is declared to rank lower than savingsLevel in the partial order of lock levels.The checkingAccount belongs to checkingLevel ,while the savingsAccount belongs to savingsLevel .The type checker statically ensures that threads acquire these locks in the descending order of lock levels.
Methods in our system may contain accesses clauses to specify assumptions that hold at method boundaries.The methods of the Account class each have an accesses clause that speci?es that the methods access the this Ac-count object without synchronization.To prevent data races,our type checker requires that the callers of an Ac-count method must hold the lock that protects the corre-sponding Account object before the callers can invoke the Account method.
Methods in our system may also contain locks clauses.The methods of the CombinedAccount class contain a locks clause to indicate to callers that they may acquire locks that belong to lock levels savingsLevel or lower.To prevent deadlocks,the type checker statically ensures that callers of CombinedAccount methods only hold locks that are of greater lock levels than savingsLevel .
Figure 2presents part of a BalancedTree implemented in our type system.A BalancedTree is a tree of Nodes .Ev-
1class BalancedTree {
2LockLevel l =new;
3
Node<self:l>root =new Node;4}
56class Node<self:v>{
7
tree Node<self:v>left;8
tree Node<self:v>right;910//this this
11///\/\12//...x (v)
13///\-->
/\14//v y u x
15
///\/\16
//u w
w y
1718synchronized void rotateRight()locks(this){
19final Node x =this.right;if (x ==null)return;20synchronized (x){
21
final Node v =x.left;if (v ==null)return;22synchronized (v){
23final Node w =v.right;24v.right =null;25x.left =w;
26
this.right =v;27
v.right =x;
28}}}29 (30)
}
Figure 2.Tree Example
ery Node object is declared to be protected by its own lock.To prevent data races,the type checker statically ensures that a thread holds the lock on a Node object before ac-cessing the Node object.The Node class is parameterized by the formal lock level v .The Node class has two Node ?elds—left and a right .The Nodes left and right also be-long to the same lock level v .
Our system also allows programmers to use recursive tree-based data structures to further order the locks that belong to the same lock level.In the example,the key word tree indicates that the Nodes left and right are ordered lower than the this Node object in the partial order.To prevent deadlocks,the type checker statically veri?es that the rota-teRight method acquires the locks on Nodes this ,x and v in the tree-order.The rotateRight method in the example performs a standard rotation operation on the tree to restore the tree balance.The type checker uses an intra-procedural intra-loop ?ow-sensitive analysis to statically verify that the changing of the partial order does not lead to deadlocks.Our type system statically veri?es the absence of both data races and deadlocks in the above examples.More de-tails about the type system will appear in (Boyapati et al.,November 2002).
References
Boyapati,C.,Lee,R.,&Rinard,M.(November 2002).Ownership types for safe programming:Preventing data races and deadlocks.Object-Oriented Programming,Systems,Languages,and Applications (OOPSLA).
正在阅读:
Safe Concurrent Programming in Java07-25
3.3细胞核--系统的控制中心06-09
网络操作系统模拟试卷104-06
供货进度计划及保证措施的承诺08-07
6方寸世界 美丽的集邮册04-12
杜郎口中学参观学习感受02-11
23、带上她的眼睛优秀教学设计05-09
感恩母亲(1)07-08
- 1AOP(Aspect Orient Programming)面向切面编程
- 2High-Productivity Stream Programming For High-Performance Systems
- 3安全文明施工费(Safe and civilized construction cost)
- 4VPO.SAFE.3.1.02.高空作业Working at Heights - 图文
- 5On the Flexibility of Constraint Programming Models From Single to Multiple Time Windows fo
- 6第一部分SAS Programming总结
- 7Two dynamic programming methodologies in very large scale neighborhood search applied to th
- 8FE-SAFE使用Abaqus的fil文件进行疲劳运算(一)
- 903.Lab02-2.LC-2K Assembly-Language Programming
- 10C++程序设计课本翻译programming - in - C++-紧缩版
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- Programming
- Concurrent
- Safe
- Java
- 成都德语培训机构:德语的由来与典故
- 高考作文素材:精美散文(76)
- 大学关于诚信的英语演讲稿
- (仅供参考)FAT文件系统的组织结构
- 2019—2020学年上学期六年级道德与法治课程纲要(1)
- 个人专业技术工作总结
- 西师版二年级数学上册教学计划
- 日本东京大学Fukuyama组10年组会2012有机反应机理习题(适合研究生)
- 2015年南宁市中考数学试题及答案(详细解析版)
- M1P4 Grammar2 Subject and Verb Agreement
- 官方最新版《实用综合教程 2》总主编 王守仁 课后习题答案全解 上海外语教育出版社 unit1—10
- 中公独家秘笈:突破高分文章必杀技之标题篇
- API石油钢管的超声波探伤原理
- 进程调度实验报告
- 刺绣衬衫出口环境分析报告
- 凿岩机钻眼工安全操作规程通用版
- 含分布式电源配电网规划的研究现状及发展趋势
- 政治期末考试命题双向细目表
- “三公”经费和会议费审计问题与对策
- 新目标英语七年级下册1---2 单元检测题