1 / 13

CSCI 6900: Design, Implementation, and Verification of Concurrent Software

CSCI 6900: Design, Implementation, and Verification of Concurrent Software. Eileen Kraemer August 24 th , 2010 The University of Georgia. Java Threads & Concurrency, continued. Liveness Deadlock Starvation and Livelock Guarded Blocks Immutable Objects A Synchronized Class Example

frisco
Télécharger la présentation

CSCI 6900: Design, Implementation, and Verification of Concurrent Software

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24th, 2010 The University of Georgia

  2. Java Threads & Concurrency, continued • Liveness • Deadlock • Starvation and Livelock • Guarded Blocks • Immutable Objects • A Synchronized Class Example • A Strategy for Defining Immutable Objects • High Level Concurrency Objects • Lock Objects • Executors • Executor Interfaces • Thread Pools • Concurrent Collections • Atomic Variables

  3. Immutable Objects • state cannot change after it is constructed. • useful in concurrent applications. • Since they cannot change state, they cannot be : • corrupted by thread interference • observed in an inconsistent state.

  4. SynchronizedRGB • See code handout … • See any problems???

  5. What if …. Thread 1: SynchronizedRGB color = new SynchronizedRGB(0, 0, 0, "Pitch Black"); ... //Statement 1 intmyColorInt = color.getRGB(); //Statement 2 String myColorNamecolor.getName(); Thread 2: //In between .. Color.set(someColor);

  6. Solution for a mutable objects synchronized (color) { intmyColorInt = color.getRGB(); String myColorName = color.getName(); }

  7. Immutable objects … • Don't provide "setter" methods — methods that modify fields or objects referred to by fields. • Make all fields final and private. • Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods. • If the instance fields include references to mutable objects, don't allow those objects to be changed: • Don't provide methods that modify the mutable objects. • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

  8. To make SynchronizedRGB immutable: • There are two setter methods in this class. The first one, set, arbitrarily transforms the object, and has no place in an immutable version of the class. The second one, invert, can be adapted by having it create a new object instead of modifying the existing one. • All fields are already private; they are further qualified as final. • The class itself is declared final. • Only one field refers to an object, and that object is itself immutable. Therefore, no safeguards against changing the state of "contained" mutable objects are necessary.

  9. See modified class ….

  10. High Level Concurrency Objects • Lock objects • Executors • Concurrent collections • Atomic variables

  11. Lock objects • “synchronized” uses simple type of reentrant lock • Other locking idioms can be found in: • java.util.concurrent.locks • Basic interface: Lock • Only one thread can own at a time • Have associated Condition objects • Support a wait/notify mechanism • Special features • Can check if lock is available (tryLock) and then back out • lockInterruptibly – backs out if another thread sends interrupt before lock is acquired

  12. New & improved Alphonse and Gaston • See Safelock example

  13. Thursday … • Executors • Concurrent Collections • Atomic Variables

More Related