Tuesday, December 7, 2010

Start and Run ..... Why do we need both ???

This post is about Java Threads. Usual way of starting a Thread ( As all the Java books mention ), is to call start() method on a Thread object. Consider the following code

public class ThreadExample
{
class ThreadImpl implements Runnable
{
public void run()
{
System.out.println("Inside run method");
}
}

public static void main(String[] ar)
{
Thread t1 = new Thread(new ThreadImpl (),"T1");
Thread t2 = new Thread(new ThreadImpl (),"T2");
t1.run(); // This code doesn't start the Thread, instead calls the method run() on "t1".
t2.run();
}
}

Here, instead of t1.start(), we have used t1.run().

When start() method is called Thread is created by the underlying platform and scheduler takes care of all such Threads now on. But when we call run(), it is just another method on a Java object and is not given to Thread scheduler.

Conclusion: If you want to write a multithreaded program, then always use start() method, else you can use run().

Proof for above statement: According to Thread design document by ORACLE (Sun), A Thread in the DEAD state can never go back to RUNNABLE state. That means, it is impossible to call start() method twice on a Thread, as shown below.

Thread t2 = new Thread(new ThreadImpl (),"T2");
t2.start();
t2.start(); // Throws an Exception,as Thread cannot go to RUNNABLE from DEAD state.

Where as, run() method can be called as many times as you want like any other method.

Hence Proved.

Java Threads - Implements Runnable V/s extends Thread


Java Threads - Implements Runnable V/s extends Thread class

Threads are very important part of any computer system, that wants to do multitasking. Threads are called low-weight processes. A process is a program under execution. Program has a text part, data part. It has memory to use for it's execution. Threads share this memory, so communication between threads is very simple and not costlier compared to IPC.

Java provides support for multi threading and also in java it is very easy to play with them.

The below article is for people who already know how to implement a Thread in java. Java gives users, 2 options. Either they have to implement Runnable interface Or they have to extend from Thread class. Only then any class, say "X" can be Thread scheduled.

Thread scheduler is part of OS and not part of JVM. So the ways threads behave changes from platform to platform.

Why do we have 2 ways of creating Thread classes ?

First thing we have to keep in mind that, Java doesn't support multiple inheritance. i.e, "class A extends B,C" is not true, where as "class A implements B,C" is true.

So if a class X extends Thread, then it cannot extend from any parent (So sad :-( ). In order to avoid this, Runnable interface is defined and it is always better to use interfaces than extending from a parent.









Monday, January 11, 2010

Eclipse Problems and solutions

Problem: Eclipse opens the project and shows and ‘X’ mark at the project folder. The entire java build path etc is proper. Cleaning the propjet works, but building the project fails. The progress bar stops after showing “Copying resources to folder”.

Solution: Goto project --> properties --> Build Path . Change the output folder to some other name ( usually it will be bin , change to bin1 etc ).

Root Cause : Not known .

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>


Problem: A java error comes .... "ClassNotFoundException a.b.c.DOMParser" .. During compile time / run time

Solution: Find out the jar file for this class and append the path to the classpth variable. Ex: add D:\Hui\xcers.jar to classpath to support DOMParser

Root Cause : No class file found in the class path .


<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>



Problem: Tomcat was getting started and closed immediately without server stating completely.

Solution: The CLASSPATH environment variable was having 2 values "D:\xx\xxxx\tools.jar;D:\xx\xxxx\xcerse.jar"
I removed the last one and the semicolon.Tomcat is coming up and running.

Root Cause: NO idea....

<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>


Problem: When compiled a project, an error is shown ==> "

The hierarchy of the type is inconsistent"


Solution: Find the classes which the faulty class extends and make those base classes compile clean / If they are missed in the project source files, add them.


Root Cause : Suppose the class name here is A and A extends B, then the error is shown ifB is not compile clean / It is missed in the project source files.