Thursday, April 29, 2010

implements Runnable vs extends Thread:Java

In order to implement Multi-Threading in Java, one must implement the Runnable interface OR extend Thread class. I have read in many forums people asking which one to use, does one of the above ways have any advantages over the other, similar questions.

I also sometimes pondered on this. I just thought for a while, i realized that one cannot extend more than one class in Java (unlike C++).

So, if one has to write a class that does multi threading, one has to prefer doing the same using the implementing the Runnable interface as one is not really worried about "extending" features of Thread class, rather we are interested in using multi-threading features.

It also adds up that, implementing an interface gives us an option of extending a class. But if, we extend a class(Thread) we do not have an option of extending other class (if required).

Here is a good article that i found on Google:

Friday, April 23, 2010

Generate Hash of a file: Java

I google'ed/bing'ed a lot to find articles on generating a hash of a file and found none. So, i decided to write one myself.

In this article i explain about generating a hash for a file in Java.

We make use of "*" API classes to achieve the task.

In the following program i am generating the hash of the java source of this program (it need not be so, you can generate hash for any file).

The method

getInstance in 

MessageDigest class takes name of the hashing technique/Algorithm as a paramter

refer Java API at