Class Note for CMPSCI 691 at UMass(11)
Class Note for CMPSCI 691 at UMass(11)
Popular in Course
Popular in Department
This 2 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Massachusetts taught by a professor in Fall. Since its upload, it has received 14 views.
Reviews for Class Note for CMPSCI 691 at UMass(11)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
CMPSCI 691W Parallel and Concurrent Programming Spring 2006 Lecture 5 February 13 Lecturer Emery Berger Scribe Dennis Gove 51 Overview This lecture is the second of two concerning concurrency in Java speci cally concurrency improvements in Java 15 also known as Java 5 We begin by discussing some problems with standard locks and then delve into some java implementations meant to solve those problems including atomic integers 52 Locks A quick review of locks will remind us that locks provide safety for shared data That is only one thread at at time will be allowed to access a given piece of memory You7ll recall that data locking is accomplished in java via a special command synchronizedvariable one can also synchronize entire methods 521 Problems with Locks Thatls right you can t have your cake and eat it too there are some problems with java locks First you can t attempt to acquire a lock and then stop trying after some timeout Second there is no concept of a readerwriter lock Third all locks are reentrantable allows recursion but very overhead intensive Fourth any method can call synchronized which leaves us with almost no access control Fifth we only have block structured locking synchronized do what you want until the end of the block Sixth priority inversion can occur if a lowpriority process acquires a lock then gets switched out for a higherpriority process and the highpriority process must block because it needs the same lock Seventh convoying can occur where everyone who needs a lock must wait for the slowest process to release the lock think of a convoy of fruit trucks where the speed of the lead truck maxes at 50mph while the nal truck ies at 80mps the nal truck must slow down to 50mph Don7t worry there are some solutions 522 Some Solutions 0 javautilconcurrentlocks The package concurrentlocks provides us with the ability to use the familiar lock unlock as well as trylock trylocktime timeunit reentrant locks and reentrantReadWrite locks The trylock will return if it cannot acquire the lock while the trylocktime timeunit will continually try for time timeunits 5 TimeUnitSECONDS means it will try for 5 seconds There is support for rolling your own using conditions though this is discouraged unless you are an expert discouraged to the tune of don t do it ever 0 javautilconcurrentatomic The package javautilconcurrentatomic provides us with the ability to get at some hardware level atomic operations testand set etc which are the building blocks for nonblocking data structures 51 52 Lecture 5 February 13 7 Atomiclnteger 9 setint 9 get addAndGetint7 incrementAndGet7 getAndAddint7 getAndlncrement compareAndSeteXpected7 update 7 Atomic Reference setV newValue get 7 getAndSet V newValue compareAndSeteXpected7 update The atomicReference can lead to the StackABA problem where we are poping things off a stack whereby we popA7 switch process somebody else popsB7 switch process back to us we pushAi This will lead to a situation where the head points to B7 but should point to A see slides 915 of lecture 5 Advanced Java Concurrency for a pictorial repi What is the solution We could stamp our references and make sure that the stamp we expect to be there is actually the one that is there A good analagy is a le versioning system SVN7 CVS 7 AtomicStampedReference setV newValue get 7 getAndSet V newValue compareAndSeteXpected7 update 0 javainio The package javainio provides support for nonblocking io operations and other lowlevel io memory mapped byte buffers7 channels7 pipes7 selectors 7 Memory Mapped Buffers An array is mapped to a le on disk and uses VM operations to access 96 Access to buffer in memory equal a call to the disk 96 This is much faster than regular io because io is the slowest thing in the world and if we can amortize that with VM calls we are much better off 7 Selectors Same idea as select You are able to add any channels of interest and then start io operations remember that it must be con gured as nonblocking scicon gureblockingfalse When called it returns an iterator with channels ready for io operationsi
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'