Monday, May 5, 2014

Java different schedulers usage

Below are the different ways to schedule a task in java

1. Timer-
Sample code : https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeTimerTask.java

a. Task needs to extend TimerTask. So you can extend any other classes
b. It has only one execution thread. Long running tasks may delay other tasks
c. Runtime execption might kill that single thread which may cause the timer dead

2. Cron4j scheduler
Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeSchedulerTask.java

a. Task just needs to be Runnable.
b. It supports the very famous cron expression, which makes easy to understand/schedule
c. you can start and stop a schedule at any time. You can control any ongoing task.
d. Can implement our own scheduler listeners by implementing SchedulerListener interface

3. ExecuterService

Sample code: https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/PrintTimeExecutorTask.java

a. Task just needs to be Runnable
b. It can catches runtime exceptions

How to initilize the scheduler for the tasks??
https://github.com/anjaneyavsk/anjaneyaUtils/blob/master/SriniUtilsProject/src/com/srini/scheduler/ScheduleThread.java

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