click below
click below
Normal Size Small Size show me how
OCPJP711.1
Cert Obj Use java.util.concurrent Collections
[11.1.1] certification objective | Use collections from the java.util.concurrent package with a focus on the advantages over and differences from the traditional java.util collections. |
[11.1.2] The synchronizers provided in the java.util.concurrent library and their uses are listed here: . | Semaphore, Phaser, CountDownLatch ,Exchanger,CyclicBarrier |
[11.1.3] | Phaser is used to support a synchronization barrier. |
[11.1.4] | CountDownLatch allows threads to wait for a countdown to complete. |
[11.1.5] | Exchanger supports exchanging data between two threads. |
[11.1.6] | CyclicBarrier enables threads to wait at a predefined execution point. |
[11.1.7]A semaphore controls access to shared resources. A semaphore maintains a counter to specify the number of resources that the semaphore controls.acquire(), release() | Access to the resource is allowed if the counter is greater than zero, while a zero value of the counter indicates that no resource is available at the moment and so the access is denied. acquire decrements and release increments counter |
[11.1.8] See Book A | Table 14-1. Important Methods in the Semaphore Class page 436 bk A |
[11.1.9] How Semaphore is used (2 ATMs and 5 people example) | class A extends Thread { Semaphore atmMachines = new Semaphore(number of ATM Machines) |
[11.1.10] How CountDownLatch is used | This synchronizer allows one or more threads to wait for a countdown to complete. This countdown could be for a set of events to happen or until a set of operations being performed in other threads completes. page 438 bk A |
[11.1.11] Sample CountDownLatch (Runners in race.) | The class Runner simulates a runner in a running race waiting to start running.It waits for the race to start by calling the await() method on the CountDownLatch object passed through the constructor. Counter = value 5, ie the countdown is from 5 to 0. |
[11.1.12] In the main() method, you create Runner objects; these three threads wait on the counter object. For each second, you call the countDown() method, which decrements count by 1.Once the count reaches zero, all three waiting threads are released. | Note: In this program, the sequence in which Joe, Carl, or Jack is printed cannot be predicted since it depends on thread scheduling. So, if you run this program, you may get these three names printed in some other order. |
[11.1.13] The Exchanger class is meant for exchanging data between two threads. What Exchanger does is something very simple: it waits until both the threads have called the exchange() method. | When both threads have called the exchange() method, the Exchanger object actually exchanges the data shared by the threads with each other. This class is useful when two threads need to synchronize between them and continuously exchange data. |
[11.1.14] The DukeThread class runs as an independent thread. It talks to CoffeeShopThread that runs independently. The chat is achieved by exchanging messages through a common Exchanger object that synchronizes the chat between them. | The message printed are the "responses" received from CoffeeShopThread class KnockKnock { ...main(String []args) { Exchanger sillyTalk = new Exchanger(); new CoffeeShopThread(sillyTalk).start(); new DukeThread(sillyTalk).start(); } } |
[11.1.15] CyclicBarrier (methods in bk a page 443) | There are many situations in concurrent programming where threads may need to wait at a predefined execution point until all other threads reach that point. CyclicBarrier helps provide such a synchronization point; |
[11.1.16]main() method: create a CyclicBarrier object. two arguments: number of threads to wait for, thread to invoke when all threads reach the barrier. If four players to wait for, create four threads, with each thread representing a player. | second argument for CyclicBarrier constructor is MixedDoubleTennisGame - this thread represents the game, which when the four players are ready. |
[11.1.17] | Inside the run() method for each Player thread, you call the await() method on the CyclicBarrier object. Once the number of awaiting threads for the CyclicBarrier object reaches four, the run() method in MixedDoubleTennisGame is called. |
[11.1.18] Phaser (bk A page 445) is a useful feature when few independent threads have to work in phases to complete a task. | Synchronization point is needed for the threads to work on a part of a task, wait for others to complete other part of the task, and do a sync-up before advancing to complete the next part of the task. |
[11.1.19] Eg Processing a delivery order in a small coffee shop. Assume that there are only three workers: a cook, a helper, and an attendant. To simplify the program logic, assume that each delivery order consists of three food items. | Completing a delivery order consists of preparing the three orders one after another. To complete preparing a food item, all three workers—the cook, the helper, and the attendant—should do their part of the work. |
[11.1.20] Three categories of collections | copy-on write collections, concurrent collections, blocking queues |
[11.1.21]copy on write collections (must always be used with Iterator) & concurrent collections | CopyOnWriteArrayList and CopyOnWriteArraySet as well as concurrent collections are designed to be similar to traditional collections, but made for efficiency and thread-safety |
[[11.1.22] 5 concurrent collections | CHM, CLD, CLQ, CSLM, CSLS for concurrent use without locking |
[11.1.23]Who is weakly consistent | Iterators of these 5 CM impl (SHM & CSLM) add atomic putIfAbsent, remove, replace to Map interface |
[11.1.24] seven blocking queue implementations | ABQ, LBQ, LBD, PBQ, DQ, LTQ, SQ |
[11.1.25] LinkedTransferQueue | is a Q, is a SQ, is a CLQ more efficient than j.u.c. queues |
[11.1.26]ForkJoinPool, ForkJoinTask |