AZK CP java.util.concurrent_Synchronizers

Home > Preview

The flashcards below were created by user flashsmilenet on FreezingBlue Flashcards.


  1. Synchronizers offer...
    ... high-level ways of synchronizing the interactions between multiple threads
  2. synchronizer classes:
    • Semaphore
    • CountDownLatch
    • CyclicBarrier
    • Exchanger
    • Phaser
  3. Semaphore
    - is ...
    - Implements ...
    • - is Synchronizer class
    • - Implements the classic semaphore
  4. Semaphore has .?. constructors:
    • Semaphore(int num)
    • Semaphore(int num, boolean how)
  5. Semaphore(int num)
    - is...
    - "num" ...
    • - is constructor of Semaphore - class
    • - "num" is number of threads that can access a shared resource
    • - waiting threads are granted a permit in an undefined order
  6. Semaphore(int num, boolean how)
    - is...
    - num ...
    - how ...
    • - is constructor of Semaphore - class
    • - "num" is number of threads that can access a shared resource
    • - "how" = true - waiting threads are granted a permit in the order in which they requested access.
  7. To acquire a permit, using Semaphore - class call...
    • 1) void acquire( )
    •    - throws InterruptedException
    •    - acquires one permit (Most often)
    • 2) void acquire(int num)
    •    - throws InterruptedException
    •    - acquires num permits
  8. void acquire( )
    - is ...
    - to ...
    • - is a method of the Semaphore - class
    • - to acquires one permit (Most often)
    • - throws InterruptedException
  9. void acquire(int num)
    is ...
    to ...
    • - is a method of the Semaphore - class
    • - to acquires num permits
    • - throws InterruptedException
  10. If the permit cannot be granted at the time of the call acquire( ) method
    - the invoking thread suspends until the permit is available
  11. To release a permit, call...
    • 1) void release( )
    •    - releases one permit
    • 2) void release(int num)
    •    - releases num permits
  12. void release( )
    - is ...
    - to ...
    • - is a method of the Semaphore - class
    • - to release one permit
  13. void release(int num)
    - is ...
    - to ...
    • - is a method of the Semaphore - class
    • - to release num permits
  14. Create Semaphore with
       - 1 threads that can access a shared resource at any one time
       - waiting threads are granted a permit in an undefined order
    Semaphore sem = new Semaphore(1);
  15. Create Semaphore with
       - 1 threads that can access a shared resource at any one time
       - Waiting threads are granted a permit in the order in which they requested access.
    Semaphore sem = new Semaphore(1, true);
  16. CountDownLatch
    - is
    - to
    • - is synchronizer class
    • - to make thread to wait until a specified number of events have occurred
  17. to make thread to wait until a specified number of events have occurred use...
    CountDownLatch synchronizer class
  18. CountDownLatch - class is created with...
    a count of the number of events
  19. Using CountDownLatch
    - Each time an event happens...
    - When the count reaches zero...
    • - Each time an event happens, the count is decremented
    • - When the count reaches zero, the latch opens.
  20. CountDownLatch has .?. constructor(s):
    • has only one constructor:
    •    CountDownLatch(int num)
    • - num specifies the number of events that must occur to open latch
  21. CountDownLatch(int num)
    is...
    num ...
    • - CountDownLatch constructor
    • - num specifies the number of events that must occur to open latch
  22. Create CountDownLatch with 5 events that must occur to open latch
    CountDownLatch cdl1 = new CountDownLatch(5);
  23. CountDownLatch methods:
    • 1) void    await( )
    •    - thread will stop and wait until the count associated with the invoking invoking CountDownLatch within main thread reaches zero
    •    - throws InterruptedException
    • 2) boolean await(long wait, TimeUnit tu)
    •    - waits only for the period of time specified by "wait"
    •    - "tu" represent time unit
    •    - returns false if the time limit is reached
    •    - returns true if the countdown reaches zero
    • 3) void countDown()
    •    - To signal an event
    •    - Each call to countDown( ) decrements the count associated with the invoking object
    • 4) long getCount()
    •    - Returns the current count.
  24. void    await( )
    • - is a CountDownLatch method
    • - thread will stop and wait until the count associated with the invoking
    • - throws InterruptedException
  25. boolean await(long wait, TimeUnit tu)
    • - is a CountDownLatch method
    • - waits only for the period of time specified by "wait"
    •    - "tu" represent time unit
    •    - returns false if the time limit is reached
    •    - returns true if the countdown reaches zero
  26. void countDown()
    • - is a CountDownLatch method
    • - To signal an event
    • - Each call to countDown( ) decrements the count associated with the invoking object
  27. long getCount()
    • - is a CountDownLatch method
    • - Returns the current count.
  28. To wait on the latch, a thread calls...
    • await( ) method which has 2 forms:
    • 1) void    await( )
    •    - throws InterruptedException
    •    - thread will stop and wait until the count associated with the invoking CountDownLatch reaches zero
    • 2) boolean await(long wait, TimeUnit tu)
    •    - waits only for the period of time specified by "wait"
    •    - "tu" represent time unit
    •    - returns false if the time limit is reached
    •    - returns true if the countdown reaches zero
  29. Using CountDownLatch
       - To signal an event...
    • call the countDown( ) method
    • void countDown()
    •    - Each call to countDown( ) decrements the count associated with the invoking object
  30. CyclicBarrier
    - is...
    - to ...
    • - synchronizer class
    • - to enable a group of threads to wait at a predefined execution point.
  31. Using CyclicBarrier
    - set of two or more threads must...
    - defines...
    • - set of two or more threads must wait at a predetermined execution point until all threads in the set have reached that point.
    • - a synchronization object that suspends until the specified number of threads has reached the barrier point
  32. CyclicBarrier has .?. constructor(s)
    • - has 2 constructors:
    • 1) CyclicBarrier(int numThreads)
    •    - numThreads - number of threads (parties) that must reach the barrier before execution continues
    • 2) CyclicBarrier(int numThreads, Runnable action)
    •    - action - thread that will be executed when the barrier is reached.
  33. CyclicBarrier(int numThreads)
    • - is a constructor of CyclicBarrier synchronizer class
    • - numThreads - number of threads (parties) that must reach the barrier before execution continues
  34. CyclicBarrier(int numThreads, Runnable action)
    • - is a constructor of CyclicBarrier synchronizer class
    • - numThreads - number of threads (parties) that must reach the barrier before execution continues
    • - action - thread that will be executed when the barrier is reached.
  35. Create a CyclicBarrier object, specifying 3 threads that you will be waiting for and specifying thread that will be executed when the barrier is reached
    CyclicBarrier cb = new CyclicBarrier(3, new BarAction());
  36. Using CyclicBarrier
    - When each thread reaches the barrier, have it call
    When each thread reaches the barrier, have it call await( ) on that object.
  37. await( )
       - is
       - to
    • - CyclicBarrier method
    • - to call when each thread reaches the barrier
  38. Calling await( ) this will ... until ...
    Calling await( ) this will pause execution of the thread until all of the other threads also call await( )
  39. await( ) method has the following two forms:
    • 1) int await( )
    •    - throws
    •       - InterruptedException
    •       - BrokenBarrierException
    •    - waits until all the threads have reached the barrier point
    •    - return a value that indicates the order that the threads arrive at the barrier point
    • 2) int await(long wait, TimeUnit tu)
    •    - throws
    •       - InterruptedException
    •       - BrokenBarrierException
    •       - TimeoutException
    •    - waits only for the period of time specified by wait
    •    - "tu" - units represented by wait
    •    - return a value that indicates the order that the threads arrive at the barrier point
  40. Exchanger
    - is...
    - to ...
    - synchronizes ...
    - it waits ...
    • - generic synchronizer class
    • - to exchange data between two threads.
    • - synchronizes the exchange   of the data.
    • - it waits until two separate threads call its exchange( ) method
  41. When two separate threads call its exchange( ) method...
    - it exchanges the data supplied by the threads
  42. Exchanger is a...
    • - generic class
    • - Exchanger<V>
    •    - V - type of the data being exchanged
  43. Exchanger has .?. method(s)?
    ... ?
    • - has only 1 method - exchange( )
    • - which has 2 forms:
    •    1) V exchange(V objRef)
    •    2) V exchange(V objRef, long wait, TimeUnit tu)

  44. V exchange(V objRef)
    - is ...
    - throws ...
    - objRef ...
    - returns ...
    - Waits for ...
    • - is one of the form of the method of the Exchanger<V> generic class
    • - throws InterruptedException
    • - objRef is a reference to the data to exchange
    • - returns data received from the other thread
    • - Waits for another thread to arrive at this exchange point (unless the
    • current thread is interrupted) and then transfers the given object to it, receiving its object in return.
  45. V exchange(V objRef, long wait, TimeUnit tu)
    - is ...
    - trows ...
    - objRef
    • - is one of the form of the method of the Exchanger<V> generic class
    • - throws InterruptedException
    • - objRef is a reference to the data to exchange
    • - returns data received from the other thread
    • - Waits for another thread to arrive at this exchange point (unless the
    • current thread is interrupted or the specified waiting time "wait" elapses) and then transfers the given object to  it, receiving its object in return.
  46. When exchange( ) method succeed?
    exchange( ) method won’t succeed until it has been called on the same Exchanger object by two separate threads
  47. Phaser
    - is ...
    - to ...
    • - is synchronizer class
    • - to synchronize threads that represent one or more phases of activity.
  48. Phaser
    - works a bit like...
    - lets you define a synchronization object that...
    • - works a bit like a CyclicBarrier (enables a group of threads to wait at a predefined execution point) except that it supports multiple phases
    • - lets you define a synchronization object that waits until a specific phase has completed, then advances to the next phase and waits until that phase is completed ...
  49. Phaser defines 4 constructors:
    • 1) Phaser()
    •    - Creates a new phaser with initial phase number 0.
    • 2) Phaser(int parties)
    •    - Creates a new phaser with the given number of registered unarrived parties and initial phase number 0.
    • 3) Phaser(Phaser parent, int parties)
    •    - Creates a new phaser with the given parent and number of registered unarrived  parties
    • 4) Phaser(Phaser parent)
    •    - Equivalent to Phaser(parent, 0).
    •    - Creates a new phaser with the given parent and 0 of registered unarrived  parties
  50. Phaser()
    is
    that
    • - Phaser constructor
    • - Creates a new phaser with initial phase number 0.
  51. Phaser(int parties)
    • - Phaser constructor
    • - Creates a new phaser with the given number of registered unarrived parties and initial phase number 0.
  52. Phaser(Phaser parent, int parties)
    is
    that
    • - Phaser constructor
    • - Creates a new phaser with the given parent and number of registered unarrived  parties
  53. Phaser(Phaser parent)
    is
    that
    • - Phaser constructor
    • - Equivalent to Phaser(parent, 0).
    • - Creates a new phaser with the given parent and 0 of registered unarrived  parties
  54. arrive methods of Phaser (3)
    • 1) int     arrive()
    •    - Arrives at this phaser, without waiting for others to arrive.
    •    - Returns the current phase number or negative value if the phaser has been terminated
    • 2) int     arriveAndAwaitAdvance()
    •    - Arrives at this phaser and awaits others.
    •    - Returns the current phase number or negative value if the phaser has been terminated
    • 3) int     arriveAndDeregister()
    •    - Arrives at this phaser and deregisters from it without waiting for others to arrive.
    •    - Should be called only by a registered party
    •    - Returns the current phase number or negative value if the phaser has been terminated
  55. await methods of Pahser (2)
    • 1) int     awaitAdvance(int phase)
    •    - To wait for a specific phase
    •    - "phase" - phase number on which awaitAdvance( ) will wait
    •    - It will return immediately if
    •       - if the argument passed to phase is not equal to the current phase
    •       - if the phaser is terminated
    •    - This method should be called only by a registered party
    • 2) int     awaitAdvanceInterruptibly(int phase)
    •    - Interruptible version of the method awaitAdvance(int phase)
    •    - except it throwing InterruptedException if interrupted while waiting
  56. register methods of Phaser
    • 1) int     register()
    •    - To register parties after a  Phaser  has been constructed
    •    - Adds a new unarrived party to this phaser.
    •    - Returns the phase number of the phase to which it is registered
    • 2) int     bulkRegister(int parties)
    •    - To register more than one party
    •    - Adds the given number of new unarrived parties to this phaser.
  57. get methods of Phaser (5)
    • 1) int     getPhase()
    •    - Returns the current phase number.
    • 2) int     getRegisteredParties()
    •    - Returns the number of parties registered at this phaser.
    • 3) int     getArrivedParties()
    •    - Returns the number of registered parties that have arrived at the current phase of this phaser
    • 4) int     getUnarrivedParties()
    •    - Returns the number of registered parties that have not yet arrived at the current phase of this phaser.
    • 5) Phaser     getParent()
    •    - Returns the parent of this phaser, or null if none.
  58. Termination methods of Phaser (2)
    • 1) boolean isTerminated()
    •    - Returns true if this phaser has been terminated.
    • 2) void     forceTermination()
    •    - Forces this phaser to enter termination state.
  59. Method of Phaser to override
    • protected boolean    onAdvance(int phase, int numParties)
    •    - to execute a specific number of phases and then stop
    •    - Is called by the run time when a Phaser advances from one phase
    •    - You must override the onAdvance( ) method
    •    - "phase" - current phase number prior to being incremented
    •    - "numParties" - number of registered parties
    •    - must return:
    •       - true - To terminate the phaser (default)
    •       - false - To keep the phaser alive
  60. int     arrive()
    • - method of Phaser
    •    - Arrives at this phaser, without waiting for others to arrive.
    •    - Returns the current phase number or negative value if the phaser has been terminated
  61. int     arriveAndAwaitAdvance()
    • - method of Phaser
    • - Arrives at this phaser and awaits others.
    •    - Returns the current phase number or negative value if the phaser has been terminated
  62. int     arriveAndDeregister()
    • - method of Phaser
    • - Arrives at this phaser and deregisters from it without waiting for others to arrive.
    •    - Should be called only by a registered party
    •    - Returns the current phase number or negative value if the phaser has been terminated
  63. int     awaitAdvance(int phase)
    • - method of Phaser
    • - To wait for a specific phase
    •    - "phase" - phase number on which awaitAdvance( ) will wait
    •    - It will return immediately if
    •       - if the argument passed to phase is not equal to the current phase
    •       - if the phaser is terminated
    •    - This method should be called only by a registered party
  64. int     awaitAdvanceInterruptibly(int phase)
    • - method of Phaser
    • - Interruptible version of the method awaitAdvance(int phase)
    •    - except it throwing InterruptedException if interrupted while waiting
  65. int     register()
    • - method of Phaser
    • - To register parties after a  Phaser  has been constructed
    •    - Adds a new unarrived party to this phaser.
    •    - Returns the phase number of the phase to which it is registered
  66. int     bulkRegister(int parties)
    • - method of Phaser
    • - To register more than one party
    •    - Adds the given number of new unarrived parties to this phaser
  67. int     getPhase()
    • - method of Phaser
    • - Returns the current phase number.
  68. int     getRegisteredParties()
    • - method of Phaser
    • - Returns the number of parties registered at this phaser.
  69. int     getArrivedParties()
    • - method of Phaser
    • - Returns the number of registered parties that have arrived at the current phase of this phaser
  70. int     getUnarrivedParties()
    • - method of Phaser
    • - Returns the number of registered parties that have not yet arrived at the current phase of this phaser.
  71. Phaser     getParent()
    • - method of Phaser
    • - Returns the parent of this phaser, or null if none.
  72. boolean isTerminated()
    • - method of Phaser
    • - Returns true if this phaser has been terminated.
  73. void     forceTermination()
    • - method of Phaser
    • - Forces this phaser to enter termination state.
  74. protected boolean    onAdvance(int phase, int numParties)
    • - method of Phaser
    • - to execute a specific number of phases and then stop
    •    - Is called by the run time when a Phaser advances from one phase
    •    - You must override the onAdvance( ) method
    •    - "phase" - current phase number prior to being incremented
    •    - "numParties" - number of registered parties
    •    - must return:
    •       - true - To terminate the phaser (default)
    •       - false - To keep the phaser alive
  75. Phaser defines methods:
    - arrive (3)
    - await (2)
    - register (2)
    - get (5)
    - termination (2)
    - method to override (1)
    •       Arrive(3)
    • 1) int     arrive()
    •    - Arrives at this phaser, without waiting for others to arrive.
    •    - Returns the current phase number or negative value if the phaser has been terminated
    • 2) int     arriveAndAwaitAdvance()
    •    - Arrives at this phaser and awaits others.
    •    - Returns the current phase number or negative value if the phaser has been terminated
    • 3) int     arriveAndDeregister()
    •    - Arrives at this phaser and deregisters from it without waiting for others to arrive.
    •    - Should be called only by a registered party
    •    - Returns the current phase number or negative value if the phaser has been terminated

    •       Await(2)
    • 4) int     awaitAdvance(int phase)
    •    - To wait for a specific phase
    •    - "phase" - phase number on which awaitAdvance( ) will wait
    •    - It will return immediately if
    •       - if the argument passed to phase is not equal to the current phase
    •       - if the phaser is terminated
    •    - This method should be called only by a registered party
    • 5) int     awaitAdvanceInterruptibly(int phase)
    •    - Interruptible version of the method awaitAdvance(int phase)
    •    - except it throwing InterruptedException if interrupted while waiting

    •       Register(2)
    • 6) int     register()
    •    - To register parties after a  Phaser  has been constructed
    •    - Adds a new unarrived party to this phaser.
    •    - Returns the phase number of the phase to which it is registered
    • 7) int     bulkRegister(int parties)
    •    - To register more than one party
    •    - Adds the given number of new unarrived parties to this phaser.

    •       Get(5)
    • 8) int     getPhase()
    •    - Returns the current phase number.
    • 9) int     getRegisteredParties()
    •    - Returns the number of parties registered at this phaser.
    • 10) int     getArrivedParties()
    •    - Returns the number of registered parties that have arrived at the current phase of this phaser
    • 11) int     getUnarrivedParties()
    •    - Returns the number of registered parties that have not yet arrived at the current phase of this phaser.
    • 12) Phaser     getParent()
    •    - Returns the parent of this phaser, or null if none.

    •       Termination(2)
    • 13) boolean isTerminated()
    •    - Returns true if this phaser has been terminated.
    • 14) void     forceTermination()
    •    - Forces this phaser to enter termination state.

    •       Method to override
    • 15) protected boolean    onAdvance(int phase, int numParties)
    •    - to execute a specific number of phases and then stop
    •    - Is called by the run time when a Phaser advances from one phase
    •    - You must override the onAdvance( ) method
    •    - "phase" - current phase number prior to being incremented
    •    - "numParties" - number of registered parties
    •    - must return:
    •       - true - To terminate the phaser (default)
    •       - false - To keep the phaser alive

Card Set Information

Author:
flashsmilenet
ID:
331121
Filename:
AZK CP java.util.concurrent_Synchronizers
Updated:
2017-05-06 19:29:13
Tags:
AZK CP java util concurrent Synchronizers
Folders:
AZK CP Java
Description:
AZK CP java.util.concurrent_Synchronizers
Show Answers:

Home > Flashcards > Print Preview