Wednesday, April 7, 2010

Java Thread:: Part 2

For creating any thread class has to do following 2 steps:
  1. Have direct/indirect instance of class Thread.
  2. In order to satisfy 1, you need to have directly or indirectly implement Runnable interface.
With these 2 conditions implemented, you can create thread in Java.
So, for doing these 2 things, there are 2 ways in java.

Way 1:
Class can implement Runnable interface and an instance of this class is passed to Thread class.

Way 2:
Class can extend Thread class. And then instance of super or itself will be helpful in creating any thread.

Now, we have question why these rules apply to implement java threads. Then here is the logic:

  1. As we know that "start" is the method which is called to start the thread. And this method has following signature:
    public synchronized native void start();
    As this method is native, which directly means that creation of thread, allocation of system resource goes here. And in Java thread is the only class which does that. So, this give to origination to the rule 1 i.e. have instance of Thread class.
  2. Related to second rule, i.e. implement Runnable class. Reason is that the constructor of Thread class can accept any instance of class which would implement Runnable interface. So, class has to implement the this interface.
These are 2 ways to implement the Threads in java.
Next Article:: Synchronization in java

Monday, April 5, 2010

Different Thread State

There can be following states:

  1. NEW ---> A thread that has not yet started is in this state. In this state, no system resources have been allocated as yet.
  2. RUNNABLE ---> Once start method is invoked thread enter in this state.
  3. BLOCKED ---> A thread that is blocked waiting for a monitor lock is in this state.
  4. WAITING ---> A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  5. TIME_WAITING ---> A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
  6. TERMINATED ---> A thread that has exited is in this state.
These states are related to JVM not operating system thread states.

Sunday, April 4, 2010

Java Thread:: Quick Reference

Today's topic is threads in Java. So, following things I am going to add here.
  1. Different Thread states
  2. How threads can be implemented in JAVA
  3. Different Classes that we may need
  4. Context Switching
  5. Reason why some methods has been deprecated
  6. A bit on synchronisation.

Java Exception:: Quick Reference

Exception is the way in java to indicate the error condition.

All exceptions inherit from Throwable. Throwable class has 2 branches, one is Exception and another is Error. We normally catch objects of Exception class.

try/catch/finally keywords are used to handle the exception.
One try can be followed by many catch statement. That happen when piece of code can throw more than one type of Exception and every exception is subjected to be handled in different way.


Checked and Un-checked Exception is about the coding style.
If any method is throwing any exception which is sub-class of exception (RuntimeException is oddity to this) then during the compilation it is checked whether method is declared to throw exception or not.

If exception being thrown is sub-class of RuntimeException, then compile time checking is not done. (Compile time checking means check to see if method handles or throws these exceptions)


"finally" code block is the last piece of block where you want to do the processing. It is the section of the code which is executed irrespective to the occurrence of exception. You may skip this piece of section at any time. Compilation does not mandate to have this. But as part of programming practices, the piece of code which is related to freeing the resources.

The finally clause is optional. However, each try statement requires at least one catch or a finally clause.


Cause can also be associated with the exception. I have never seen doing so. And from different documentation, I found that this is more to cope up with legacy code.


References/ More to read::
  1. http://www.artima.com/designtechniques/exceptions.html
  2. Designing with exceptions
  3. http://www.java2s.com/Article/Java/Development/Breaking_Java_exceptionhandling_rules_is_easy.htm
  4. http://www.artima.com/underthehood/exceptions.html
  5. www.cs.cmu.edu/~aldrich/papers/ecoop05exnjava.pdf
  6. Good Practices for Exception based design
  7. Checked Exceptions Good-bad

Saturday, April 3, 2010

Element Access :: Java

  1. public class can not be declared in any java file. File name should match with public class name. So, in case you are planning to write multiple class in one file (which is not a good practice.) then in the java file only 1 public class could exists.
  2. Can a class have "private access" ?
    Only nested class can have private/protected access. OUTER class can have only public/default access. Private/protected access is basically meant for the member elements.
  3. An interface can have only public/default access.
  4. Changing access of any element from HIGHER to LOWER will result into compilation error.
  5. If interface is public, then all of its member variable/methods will have same access. And access of member elements can not be changed also.
  6. Variables defined in interface are implicitly static and final.
  7. Methods that implement interfaces must be declared as public. (Fun part is if you forget to do that JAVAC is there to remind you.)

Order of different access is as follows:

PUBLIC --> Protected --> Default --> Private

Hex Editor For Ubuntu

Just to read byte code in a file, you may need one hex editor.
Working on Ubuntu, I find "hexer" as a good choice.

Do following on your machine:

apt-cache search hexeditor
sudo apt-get install hexer

If you want to try something, then refer this link:
http://ubuntuforums.org/showthread.php?t=52825

Tuesday, March 30, 2010

Ubuntu Command Reference

The idea is to document all the command that I frequently in ubuntu.

I corrupt my kernel and then I have to start from scratch. So, this page is to accommodate my daily finding.

Start from here:

Get the release information of ubuntu

lsb_release -a
OR
cat /etc/issue
OR
cat /etc/lsb-release