Saturday, April 12, 2014

Thread pool implementation in Java

Why do we need Thread pool ?

Thread pools are useful when you need to limit the number of threads running at the same time.
Also there is a performance overhead associated with creation of a thread and if there is frequent creation of threads in your application. It is better to use Thread pool instead of creating a new thread every time.


Before we start implementing the code. Lets think about what we need to implement Thread pool.


1. Pool of threads - list of threads which can execute a task.
2. Queue - a queue which can put the tasks in queue and thread can pick it up to execute the task.
3. Task - a interface to all tasks and have a method execute which will be called by a thread to complete the task.

It just nothing but a typical producer - consumer problem with Tasks (given by application) are producers and threads( in a pool) are consumers and resource being the queue.

Notes:
Queue should be a blocked queue either Java provided or own implemented, which means it should make the thread in wait. So finally Thread should be in wait  status if the queue is empty rather than looping forever.
Also thread should be completed/destroyed if ThreadPool is destroyed so that it should not hold any reasources

Sample Code:

Thread in a pool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/PoolThread.java

Task - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/Task.java

ThreadPool - https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/thread/pool/ThreadPool.java



Wednesday, April 2, 2014

Generate Java objects from String expression defined by language

Want to parse a string into java objects using some grammar like regular expression or binary operations(expressions).?????

Antlr is a good tool to use. It will generate java/c++ code easily which helps to create java objects
(http://antlr2.org/)

Below are the steps to define grammar & generate java code

Define grammar:

 Tokenization(lexer):
      Define tokens (eg. AND,OR, [a...b]*). This will be used to identify the tokens in the string given.

 Parsing(Parser):
      Define grammar/rules such that It can build the tree. Antlr will parse the given string as per the rules  and build AST tree.
 (eg. expr : expr2 (OR^ expr2)*;  pexpr :(' expr ')' -> expr;)

 TreeParser:
      Define grammar/rules to specify action at each node. These action methods can be written in java
 Now it will perform the actions specified like (construct a java object/calculate a value etc)

Generate java code :
Download the antlrworks (http://www.antlr2.org/download.html)
run the antlrworks (java -jar antlrworks-***.jar)
open the grammar files defined above and click generate

Links to sample grammar files
Lexar/Parser: https://docs.google.com/file/d/0Bzsr76b-5vDma1Jwa3dYZEZNXzg/edit
TreeParser : https://docs.google.com/file/d/0Bzsr76b-5vDmWGs4aXJoVlFqeWM/edit

This is an example of  AND/OR binary tree which can be constructed from expression

PS: This is how compilers work