Distributed Programming in Java - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Distributed Programming in Java

Description:

Multiple threads accessing resource objects ... A barrier allows multiple threads to meet up at a common point (usually cyclic) ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 43
Provided by: protonScs
Category:

less

Transcript and Presenter's Notes

Title: Distributed Programming in Java


1
Distributed Programming in Java
  • Concurrency (4)

2
Confinement
  • Uses encapsulation techniques to structurally
    guarantee that at most one activity at a time can
    possibly access a given object.
  • Instead of using dynamic locking on object,
    encapsulation statically ensures the unique
    access to a given object
  • Define methods and classes that establish
    leak-proof ownership domains so that one thread
    (at a time) can ever access a confined object.

3
Confinement techniques
  • Scoping
  • Access control
  • Data hiding and encapsulation
  • Problem is information leaking

4
Information leaking
  • A reference r to an object x can escape from
    method m
  • m passes r as an argument in a method invocation
    or object constructor
  • m passes r as the return value from a method
    invocation
  • m records r in some field that is accessible from
    another activity (static fields)
  • m releases another reference that can in turn be
    traversed to access r

5
Kinds of Confinement
  • Method confinement hide access within local
    scope, including hand-offs
  • Thread confinement confine objects to sequence
    of operations (session)
  • Object confinement confine accesses internal to
    object (host-part)
  • Group confinement resource only owned by one
    object, but can be circulated

6
Method Confinement
  • Use local variables
  • class Plotter
  • //
  • public void showNextPoint() hand off
  • Point p new Point()
  • p.x computeX()
  • p.y computeY() finish modifying
  • display(p) tail call
  • simplest best form
  • protected void display (Point p)
  • //

7
Sessions
  • Method constructs objects also be responsible for
    any cleanup operations
  • class SessionBasedService
  • //
  • public void service()
  • OutputStream output null
  • try
  • output new FileOutputStream() constru
    ct object
  • doService(output) hand-off
  • catch (IOException e)
  • handleIOFailure()
  • finally
  • try if (output ! null) output.close()
    specify when to clean up
  • catch (IOException ignore)
  • void doService(OutputStream s) throws
    IOException

8
Alternative Protocols
  • public void showNextPoint()
  • Point p new Point()
  • p.x computeX()
  • p.y computeY()
  • display(p) not tail-call
  • recordDistance(p)
  • Caller copies display(new Point(p.x, p.y))
  • Receiver copies Point localPoint new
    Point(p.x,p.y)
  • Using scalar values display(p.x, p.y) not a
    point
  • Trust a receive may promise not to modify or
    transmit object.

9
Thread Confinement
  • class ThreadPerSessionBasedService
  • //
  • public void service()
  • Runnable r new Runnable()
  • public void run()
  • OutputStream output null
  • try
  • output new FileOutputStream() c
    onstruct object
  • doService(output) hand-off
  • catch (IOException e)
  • handleIOFailure()
  • finally
  • try if (output ! null)
    output.close() specify when to clean up
  • catch (IOException ignore)

10
Object Confinement
  • Confine accesses internal to object
  • Lock for host propagates to its internal parts
    (which cannot be leaked)

11
Group Confinement
  • Multiple threads accessing resource objects
  • A group of objects, each exclusively own a given
    resource
  • Ownership may change hands over time

12
Exclusively held resources
  • If you have one, you can do something with it
  • If you have one, no one else has it
  • If you give it to someone else, then you no
    longer have it
  • If you destroy it, then on only will ever have it

13
Protocols
  • Acquire owner x establishes initial possesions
    of r
  • synchronized(this) ref r
  • Forget owner x causes Resource r not to be
    possessed by any owner
  • synchronized(this) ref null
  • Put (give) owner y sends owner x a message
    containing a reference to r
  • void put(Resource s)
  • synchronized(this) ref s

14
State-Dependent Actions
  • Remove an element from an empty queue
  • Add a byte to a full buffer
  • Withdraw from an overdrawn bank account
  • Print a file on a shared printer
  • Read an unopened file
  • Shift into reverse in a moving car

15
Policies for State-Dependent Actions
Discussed here
16
Guarded Suspension
  • Context
  • We are implementing a Monitor Object.
  • Problem
  • We can execute a method only if a certain guard
    condition holds.
  • Solution
  • Suspend its client thread so that other client
    threads can access the shared object Wait for the
    guard condition to change.

17
Guarding
  • Performing state-dependent actions
  • As in locking, acquire a lock, then wait until a
    guard condition holds
  • Check state again upon entry, because it may have
    changed already (how so?)

18
Implementing Guards
  • Every Java object has a wait set
  • Accessed via monitor methods that can only be
    invoked under synchronization
  • wait()
  • Suspends thread
  • Thread is put in wait set for target object
  • Lock for target released
  • wait(time)
  • Same as wait(), but times out

19
Implementing Guards
  • notify()
  • If one exists, a thread is resumed from the
    target's wait set (no particular one)
  • Thread must still reacquire lock
  • notifyAll()
  • Like notify(), except all threads are chosen
  • Thread.interrupt()
  • Causes wait() to abort (no synch needed!)
  • Thread resumed at the associated catch

20
Wait Sets
21
Synch
  • Implementation of locks and conditions
  • Interface for acquire/release protocols

22
Mutex
23
Mutex
24
Mutex
25
Semaphore
  • Semaphores are specialized counters
  • Initialized at some value N
  • Acquiring the semaphore decrements the value as
    long as the value is positive
  • If value is 0, callers attempting to acquire it
    are blocked until value positive
  • Releasing it increments the value
  • Mutex is a special case (with N1)

26
Semaphore
27
ResourcePool
  • For example, resource pools keep counts of
    resources (e.g. printers, threads)

28
ResourcePool
29
Channel
  • Interface for buffers, queues, etc.

30
Producer - Consumer
31
Bounded Buffer
  • A bounded buffer consists of a fixed number of
    slots (size of the buffer)
  • Cannot hold more items than slots
  • Items are put into the buffer by a producer and
    removed by a consumer thread
  • Can be used to smooth out variations in
    production/consumption rates

32
Bounded Buffer
  • Three logical states for guarding purposes (which
    guards are required?)

33
Bounded Buffer
  • Here notifications are only generated when
    transiting out of empty and full states

34
Bounded Buffer
35
Queue
  • A queue is an unbounded buffer
  • Queueing policy FIFO, PriorityQueue, ...
  • Requires that, on average, producers are no
    faster than consumers (why?)
  • Can grow otherwise to exhaust memory
  • But dont need to pre-allocate slots

36
Queue
37
Latch
  • A latch is a variable or condition that can
    change only once, from initial to final state
  • Latches obey Synch interface
  • But a single release permits all previous and
    future acquires to proceed
  • Uses event indicator, time trigger

38
Latch
39
Worker
  • Can use Latch as start signal for a group of
    worker threads that are started beforehand, and
    only later enabled (e.g., game players)

40
Barrier
  • A barrier allows multiple threads to meet up at a
    common point (usually cyclic)
  • Threads must wait for other threads to hit the
    barrier before they can proceed
  • Interface for barrier classes

41
CyclicBarrier
  • For each iteration a counter is reset to the
    number of threads to synchronize

42
CyclicBarrier
Write a Comment
User Comments (0)
About PowerShow.com