Wednesday, June 1, 2011

Shutdown Hooks

http://onjava.com/pub/a/onjava/2003/03/26/shutdownhook.html

Wednesday, April 21, 2010

Nice to know -- Good ones from the Java API

1) java.text.MessageFormat.format(String pattern,Object[] arguments) ::
This is how it works -- Original string is provided with {0}, {1} and so on, in place of them and at runtime, the value for those are picked up from the array (in order) and replaced.

Ex : format("Hello {0}. welcome to {1} programming",new String[]{"world","Java"});
yields the String
Hello world. welcome to Java programming

2)
For all of you facing the content not allowed in prolog:
http://mark.koli.ch/2009/02/resolving-orgxmlsaxsaxparseexception-content-is-not-allowed-in-prolog.html

Thursday, April 8, 2010

Best Practices

Some of Industry followed best standards:
1) Use if("Value".equals(var)) instead of the usual if(var.equals("Value")),
this will avoid a null-check on var before calling the var.equals(), the result remaining the same.
Simple yet useful.

2) Try specifying the size of the java.util.ArrayList when creating it, anyways its gonna grow when you add more elements to it.

3) Comments - They talk not just about your class or method but yourself.

Never touch or write a java file without providing comments on why you are doing it. Please don't bring up the Check-in argument here. Everyone's time is precious, (I learnt this from our Architect - Tad) don't let someone waste their worthy time on becoming a Sherlock Holmes to find what and why you had done something.

4) Do not use a,b,c.... as names for the instances. They don't signify anything. Single character names should be restricted to iteration variables in 'for' loops (or sometimes not even that, if you are using for:each).

Wednesday, April 7, 2010

!!!!! moment of the day

7 Apr 2010 - Java obfuscators (http://java-source.net/open-source/obfuscators). Use it when you are shipping your code to someone who might steal your idea.

12 Apr 2010 - You could write a java file containing nearly 40,000 lines of code and it would still compile.

21 Apr 2010 - Tortoise CVS began as a project to support CVS with a better interface. (Who knows where one might end up).

24 Apr 2010 - You could connect to a running JVM using the JDWP. This is how debuggers work.

Upcoming posts - the keyword "volatile","serialization","Shutdown Hooks","JDBC - Before connecting to a DB"

These are topics you could expect some contents sooner.

Tuesday, April 6, 2010

Daemon threads in java

Programmer A: I want a java program to keep polling my inbox and alert when a new message is received.

Programmer B: Thats simple, write a daemon thread to do it.

Programmer A: A daemon thread? How do i write one? Will it keep doing the polling job forever? What are its pre-requisites?

This is not just the case with Programmer A or Programmer B, most of the java programmers dont realise (incuding me) the power and pitfalls of a daemon thread.

Programmer A: How do I make a thread daemon?

Programmer B: Declare the setDaemon(boolean) property to true.

Programmer B's answer is partly right and partly not. Setting the daemon flag to true is just the work half done. To make a thread daemon, you need to do more.

Consider the java program,

package test;


public class DaemonThreads {

public static void main(String[] args) {

System.out.println("Start Program....");

new Daemon().start();

try {

System.out.println("Main thread going to sleep....");

Thread.sleep(100);

System.out.println("Main thread woke up....");

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("End of program....");

}

}

class Daemon extends Thread{

long count = 0L;

Daemon(){

this.setDaemon(true);

}

@Override

public void run() {

System.out.println("Count = "+ ++count);

}

}


(The main thread sleeps just to show that the daemon isn't doing anything. It is intentional and doesn't serve any other purpose.)

E:\test\Test\src>javac test/DaemonThreads.java

E:\test\Test\src>java test.DaemonThreads

Start Program....

Main thread going to sleep....

Count = 1

Main thread woke up....

End of program....

E:\test\Test\src>

It is evident that the daemon doesnt keep running, as we expected it to. The reason being, just on declaring as setDaemon(true), the thread is daemon only according to JVM (JVM doesnt consider it as a User Thread) but the job performed by the thread isnt that of a daemon.

A daemon thread is expected to be running always and does some job as long as the program is kept running. Hence this behavior is inserted into this thread prgrammatically, by using a indefinite loop, a infinite for loop or do-while(true) or the most widely used while(true) as shown below.

public void run() {

while(true)

System.out.println("Count = "+ ++count);

}

When run,the count is printed even after End of program is printed, clearly showing the behavior of a daemon which keeps running in the background irrespective of what happens in the main thread.

For those who clearly didn't understand the idea behind making the thread daemon, keep reading. The program exits even when Thread that was started by us isn't finished. (The thread should have been running till my JVM crashes as i have placed a while(true) condition, which can never be false to terminate the loop). Hence the idea is, daemon threads cannot outlive the javaw they were started in. Once all the processes started in the javaw is completed, it checks for any running User threads (note: not daemon threads, threads that don't have the daemon flag set) and when it doesn't find any, terminates the javaw, and of course the daemon that was running in it.

Hence setting the daemon flag to true doesnt just make a thread daemon, providing an infinte loop alone doesnt make a thread daemon.

Simple Ideas. Powerful Results.

Why to blog (blab)?

In more than half my day as a java programmer, I find and fascinate about lot of things in Java (and other programming languages, at times). This my first step to share my knowledge about all this with fellow developers.I am hoping that blogging will also make me ponder about lot of stuff around the programming world (Believe me, its a totally different world).