Vous êtes sur la page 1sur 2

3/31/2017 Strategy2:Immutability|Reading6:ThreadSafety|6.005.

2xCourseware|edX

MITx: 6.005.2x Advanced Software Construction in Java Help

Readings > Reading 6: Thread Safety > Strategy 2: Immutability



Bookmarks
Strategy 2: Immutability
Bookmark this page
Readings

Reading 1: Recursion Strategy 2: Immutability


Reading Exercise due Apr 24,
2017 14:00 CDT Our second way of achieving thread safety is by using immutable references and data types.
Immutability tackles the shared-mutable-data cause of a race condition and solves it simply by
Reading 2: Recursive Data
Types making the shared data not mutable.
Reading Exercise due Apr 24,
2017 14:00 CDT
Final variables are immutable references, so a variable declared nal is safe to access from
Reading 3: Regular multiple threads. You can only read the variable, not write it. Be careful, because this safety
Expressions & Grammars applies only to the variable itself, and we still have to argue that the object the variable points to
Reading Exercise due Apr 24,
2017 14:00 CDT is immutable.

Reading 4: Parser
Immutable objects are usually also threadsafe. We say usually here because our current
Generators
Reading Exercise due Apr 24, denition of immutability is too loose for concurrent programming. We've said that a type is

2017 14:00 CDT immutable if an object of the type always represents the same abstract value for its entire
Reading 5: Concurrency lifetime. But that actually allows the type the freedom to mutate its rep, as long as those
Reading Exercise due Apr 24, mutations are invisible to clients. We saw an example of this notion, called benevolent or
2017 14:00 CDT
benecent mutation, when we looked at an immutable list that cached its length in a mutable
Reading 6: Thread Safety eld the rst time the length was requested by a client. Caching is a typical kind of benecent
Reading Exercise due Apr 24,
2017 14:00 CDT mutation.

Reading 7: Sockets &


For concurrency, though, this kind of hidden mutation is not safe. An immutable data type that
Networking
Reading Exercise due Apr 24, uses benecent mutation will have to make itself threadsafe using locks (the same technique
2017 14:00 CDT
required of mutable data types), which we'll talk about in a future reading.
Reading 8: Queues &
Message Passing Stronger denition of immutability
Reading Exercise due Apr 24,
2017 14:00 CDT So in order to be condent that an immutable data type is threadsafe without locks, we need a
stronger denition of immutability:
Reading 9: Locks &
Synchronization
Reading Exercise due Apr 24, no mutator methods
2017 14:00 CDT
all elds are private and nal

Entrance Survey no representation exposure

no mutation whatsoever of mutable objects in the rep not even benecent mutation
Week 1
If you follow these rules, then you can be condent that your immutable type will also be
threadsafe.
Week 2

In the Java Tutorials, read:


Week 3
A Strategy for Dening Immutable Objects (1 page)
Week 4

Week 5
Discussion Show Discussion
Topic: Reading 6 / Strategy 2: Immutability
Week 6

https://courses.edx.org/courses/coursev1:MITx+6.005.2x+1T2017/courseware/Readings/06ThreadSafety/ 1/2
3/31/2017 Strategy2:Immutability|Reading6:ThreadSafety|6.005.2xCourseware|edX

All Rights Reserved

2012-2017 edX Inc. All rights reserved except where noted. EdX, Open edX and the edX and Open EdX logos are registered trademarks or
trademarks of edX Inc.

https://courses.edx.org/courses/coursev1:MITx+6.005.2x+1T2017/courseware/Readings/06ThreadSafety/ 2/2

Vous aimerez peut-être aussi