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

Sunday, September 7, 2008

configuring Evolution mail to work with gmail/yahoo/*

Start the evolution mail or thunderbird,for that matter.. run the wizard that adds new account,the options for use with…

1. Yahoo mail are as follows, step wise.

step 1 : specify user login.

In the email address tab type @yahoo.co.in(or .com)

step 2 : Receiving Mail options

Server type : POP
Server : pop.mail.yahoo.com
username : someone
Use secure conn.: No encryption

step 3 : Sending mail options

Server type : SMTP
Server : smtp.mail.yahoo.com
authentication : PLAIN

2. GMail

step 1 : almost same as above setup.

For GMail every thing is same except it should be SSL encryption in the palces of ‘No encryption’ and ‘PLAIN’.

step 2 : Receiving mail options

server for receiving mail is pop.gmail.com

step 3 : Sending mail options

server for sending mail is smtp.gmail.com

step 4 : Enable POP in gmail setting

Most importantly,you SHOULD enable the POP in gmail settings.
Goto GMail/settings/Forwarding and POP and enable POP.

3. IIIT Students Mail (Intranet):

everything is same as yahoo mail config,except that both the servers are ’students.iiit.ac.in’

grub errors

if you somehow delete the linux partition from windows, then it is very likely to get grub error. That means when we boot it looks like grub>

To recover windows ntloader you need only a winxp cd.
Boot your pc with cd and when blue screen appears, press 'r' for a recover console and then type 'fixmbr' in the console. Take the cd out and restart your system. you did it

If you dont have a cd or if you want to boot winxp for once:
Type the following commands at the grub command prompt
grub> rootnoverify (hd0,0) #(hd0,0) means your hard disk id in which you installed winxp
it may be (hd0,1); it depends
grub> makeactive
grub> chainloader +1
grub> boot

note: But in this method you need to type all the commands everytime you want to boot winxp

Suppose you installed windows after linux, you wont see linux option in the boot menu and starts windows automatically. Thats because windows will erase mbr.
To recover that you need first cd or rescue cd of linux. Boot from cd and type this at boot prompt:
boot: linux rescue
This will go some options which then leaves to a shell prompt. Type these commands at that prompt:
prompt: chroot /mnt/sysimage
prompt: grub-install /dev/hda #hda refers to your hard disk naming. It may be hdb or sda
prompt: exit
remove the cd and reboot it . you did it
Note:
we cant boot windows which is installed in logical drive and linux in primary drive

Thursday, May 15, 2008

installing winxp using pendrive

requirements:
  • pen drive with atleast 1GB
  • winxp installation disk
  • access to a PC with working CD-ROM drive and winrar software
  • this file solution.rar or solution.rar (both are same but links are different).
procedure:

Put your pendrive into the working PC
Download the file and unpack it on drive C
Go to the folder C:\Dos and run HpUSBformat.exe file
it will ask to format the pen drive put options like in the image below
now you have a bootable pendrive
copy all files from C:\Dos to pendrive
put winxp installation disk into CD-ROM and copy all files from CD to pendrive
now boot your system(in which you want to install winxp) with pendrive
use fdisk to see your drives and format or create partitions (if you have partitions formated already you dont need this step)
using nc commander, copy all files from pendrive to partitioned drive( type nc enter and then F5 to copy files it will ask for path give the path for more shortcuts see this )
now you have system with DOS bootable
restart your system without pendrive
type cd i386 enter and then winnt enter
it will install winxp as if we put a cd in CD-ROM (remember don't install winxp in the same partition as in we copy these files because while installing winxp it will format the partition and we will loose all the installation files)
Note:
1. we need 2 empty partitions by this method
2. your system should have the option "boot on pendrive"

Sunday, March 16, 2008

some fun with terminals

Some fun with terminals
There are two tools that can be usefull when working in console :
- script
- screen

1. script

script is a nice program that let you monitor in one or
more terminals what you type in another terminal.

Example:
- Open two X terminals in your X session or use the Linux
ttys (ctrl + alt + F1-6)

- On one terminal, type :
Code:
cd ~
mkfifo output
script -f output
- On another terminal (or more), type :
Code:
cd ~
cat output

- Then return to the first terminal and execute any
command you like (ls, cd, ping...)

The output should be appear on every terminals where
you cat output fifo.

- Exit script session by typing exit command in the first
terminal (the one which you started script command)

man script for more infos


2. screen

screen is a terminal screen manager (like a window manager
but for consoles)

Example:
- In a terminal, type : screen
screen is now started, it has opened one screen, you should
see the prompt of your shell, waiting for commands

- Name this screen with typing : [ctrl + a], then A (note that
A is uppercase, so you may need to type:
[ctrl +a], then [shift +a]
Erase the default title using backspace key and name it whith
any name you want, say "Shell 1", press key.

- Create a new screen with : [ctrl + a] [ctrl + c], name it
([ctrl + a], [shift + a]), say "Shell 2"

- To switch between these 2 screens, type [ctrl + a], then ["]
(double quote key), you should see a list of all your screens
To enter on one, just select it with the top/bottom arrow keys
and press

- To exit screen, just type exit in all your opened screens

Tips for ssh session :
Say you run a command that take long time to complete
(say updatedb or a system upgrade), you can detach your
screen, exit ssh session, let the command run on the
remote host and return to the screen later :

- ssh to your remote host, run screen, run your command,
then type : [ctrl + a], [shift + d], [shift + d]

You now are now logout of your ssh session, screen in
remote host is detached so it keep running your command

To return to your screen, ssh into your remote host, then
use : screen -r

You are now in the screen showing output of the command
you issued before.

man screen for more infos