Sunday, 31 July 2016

Java 5 BlockingQueue Features and Usage

BlockingQueue-

The BlockingQueue extends Queue.

Important Methods of Blocking Queue are-

Insert-NonBlockingQueue
1-add() - It will add specified element into BlockingQueue.No Blcoks behaviour methods apply with this method.If element will be added into BlockingQueue successful ly it will return true ,if not then will throw the exception “IllegalStateException”
2-offer(e)-true/false-will return true if element will be added ,if not added will return the false.If you will try to add Null then will throw the
exception NullPointerExceptiin

Insert Blocking
3-put(e)-Its a blocking operation ,if not space will be available ,it will wait -and as it will find the space ,it will add the specified element .
This method returns void and will throws exception NullPointerExceptiin if pass Null.
4-offer(e,long,TimeUnit)-Will wait upto specified timeunit if space is not available.If when space will be available during TimeUnnit ,it will
add and return true,if time will be elapsed then will return false.


Retrieval - Non BlockingQueue

1-peek()-  will return the head element and will not remove the Head, returned Null if no element exist.
2-poll()-   will return the head element and will removed the head., returned Null if no element exist.

Retrieval-Blocking
3-poll (long, TimeUnit)-Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element to become available.
4-take ()-It will wait until an element will be available.

int drainTo(Collection<? super E> c);
Removes all available elements from this queue and adds them to the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

int drainTo(Collection<? super E> c,int maxElements);
Removes at most the given number of available elements from this queue and adds them to the given collection. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Scenarios Where I Have used It-

Also we can use it in producer-consumer type problems
Where i have used LinkedBlockingQueue.?

How this solve the problems-
We are receiving multiples files on shared directory from an application.
A thread search the location and if files are available ,their main task is to

1)Reading the files
2)Parsed the files
3)-Format the files.
4)-Hit the URL of application -for each and every number.
5)-Save all data into database.
6)-and delete the files from shared directory and move it to backup  directory .

 Approach One-If we will create the one thread then it will take no lime to do all activities,
 because saving the data into DB will take time and hitting the application URL  will taking also times. So we come to things that ,we will read all files currently available on path.
 We have created six thread for six task and 5 LinkedBlockingQueue . All six thread are running independently.

 1)FileReaderThread - will read all files and copied those file into another directory and update database for files has been received now and it will put(File file) into LinkedBlockingQueue ,another thread(2) which read the file from LinkedBlockingQueue by using take() method and searched the
picked file from copied location (where thread one copied those files).after parsing the files it will update the DB and copied those files from directory to another directory,Now thread 3 will start their execution and read file from LinkedBlockingQueue and find it on Location (from here Thread2 copied the files).So here is best use of LinkedBlockingQueue and last thread will delete file from base-location where the file received initially.If any things goes wrong our server shutdown or any misshapen occurWe can start our execution from same point where we lost previously.because file status are also saving into DB

DIFFERENCE BETWEEN ADD() and OFFER()::
Add() means that if no space is available in queue ,it will throw exception(Exception in thread "main" java.lang.IllegalStateException: Queue full) while in case of offer() it will not throw any exception ,it will return a special values-‘FALSE’,In case of no space is available.
capacity restrictions- it means that queue capacity which we give at time of BlockingQueue creation.
Throws exception
Special value
add
offer
remove
poll
get
peek

Add(E e) Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an Exception if no space is currently available.


Offer() Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception. 

No comments:

Post a Comment