Concurrency

Rule 4:Prefer Concurrent utilities to wait and notify

Given the diffulty of using wait and notify correctly, you should use higher level concurrency
utilities provided since JAVA-5.

the higher level utilities in java.util.concurrent fall into three categories:
1.Executor framework(in Brief: Rule 3)
2.Concurrent Collection
3.Synchronizers

“Concurrent Collection”
is HIGH-PERFORMANCE CONCURRENT implementation of standard collection INTERFACES such as List,Queue and Map.
to provide HIGH-CONCURRENCY they manage their own synchoronisation internally (Rule 2).
Therefore, IT IS IMPOSSIBLE TO EXCLUDE CONCURRENT PROPERTY(bcoz of internally sync?) FROM CONCURRENT COLLECTION;LOCKING it will only SLOW THE PROGRAM.

state-dependent modify operations,

Concurrent collections make synchronized collections largely obsolete. For example, use ConcurrentHashMap in preference to Collections.synchronizedMap.
Simply replacing “synchronizedMap” with CONCURRENT MAPS can dramatically INCREASE THE of concurrent applications.

Some of the Interface were enxtended with BLOCKING OPERATIONS.which block (or wait) untill they are
successfully performed.
Example:
BlockingQueue extends Queue and adds several methods, including take, which REMOVES AND RETURN the head element
from the queue.WAITING if the queue is EMPTY.

This allows BlockingQueue to be used for WORK QUEUES (Also known as Producer-Consumer Queues), to which onw or more
PRODUCER-THREAD enqueues the work item(may be Http request) and from which one or more CONSUMER-THREADS dequeue
and process the items as they become available.

Synchronizers
are objects that enable threads to wait for one another, allowing
them to coordinate their activities.
The most commonly used synchronizers are :
“CountDownLatch” and “Semaphore”.
Less commonly used are:
“CyclicBarrier” and “Exchanger”.
The most powerful synchronizer is “Phaser”.

Countdown latches are single-use barriers that allow one or more threads to
wait for one or more other threads to do something. The sole constructor for
CountDownLatch takes an int that is the number of times the countDown method
must be invoked on the latch before all waiting threads are allowed to proceed.

Further Apllication (Kind of Quartz Job):
It is surprisingly easy to build useful things atop this simple primitive. For
example, suppose you want to build a simple framework for timing the concurrent
execution of an action. This framework consists of a single method that takes an
executor to execute the action, a concurrency level representing the number of
actions to be executed concurrently, and a runnable representing the action.

Published by

Unknown's avatar

sevanand yadav

software engineer working as web developer having specialization in spring MVC with mysql,hibernate

Leave a comment