Sunday, January 23, 2011

FAQS on threads concept on JAVA

         Answer: Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction.
The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications. 

Question: What is a thread?

Answer: In Java the Thread class represents a single independent path of execution in a Java Virtual Machine. When you run a Java program it implicitly starts a single thread of execution. The Thread class enables programmers to create additional threads and set them running. A number of threads may run in parallel, but only one is actively executed at a given moment.
The Java runtime system uses fairly complex thread scheduling mechanisms to coordinate the execution of threads, but this does not require privileged knowledge or detail level intervention by programmers. Programmers can manage the high level creation, initiation and distribution of tasks amongst threads through simple Application Programming Interface (API) methods.
The example below shows the simplest approach to thread creation and task execution; construct a new Thread with a Runnable argument and start it.

Question: How do Java threads make the environment asynchronous?

Answer: The thread mechanism in Java begins with the main entry point thread the runtime environment creates to start a Java program. When you use that initial thread create secondary threads, each one runs independently of the other. The Java virtual machine manages the execution of the threads so they behave as if they all run at the same time, in fact each thread briefly takes turns at execution.
In its simplest form there may be no communication or synchronization between multiple threads in a Java program and they each run to completion independently of each other. In this respect Java threads are fundamentally asynchronous, there is no master clock that governs when threads will run and when they synchronize variables to “catch-up” with each other.
        It is often necessary and more useful if threads do check ready states before progressing, synchronize read and write access to shared variables and call-back to each other when their work is done. This is where the synchronized keyword and the various sleep(), wait() and notify() methods are used to more closely schedule the interaction between asynchronous threads.

Saturday, January 22, 2011

Example for thread in java

                 This example illustrates how to create a thread and how to implement the thread. In this example we will see that the program prints numbers from 1 to 10 line by line after 5 seconds which has been declared in the sleep function of the thread class. Sleep function contains the sleeping time in millisecond and in this program sleep function has contained 5000 millisecond mean 5 second time. There is sleep function must caught by the InterruptedException. So, this program used the InterruptedException which tells something the user if thread is failed or interrupted.

Here is the code of the program : 
public class Threads{
  public static void main(String args[]){
    Thread th = new Thread();
    System.out.println("Numbers are printing line by line after 5 seconds : ");
      for(int i = 1;i <= 10;i++)
    catch(InterruptedException e){
      System.out.println("Thread interrupted!");

Wednesday, January 19, 2011

What is the difference between thread amd process?

                     A process is an instance of a computer program that is executed sequentially. It is a collection of instructions which are executed simultaneously at the run time. Thus several processes may be associated with the same program. For example, to check the spelling is a single process in the Word Processor program and you can also use other processes like printing, formatting, drawing, etc. associated with this program.

                A thread is a lightweight process which exist within a program and executed to perform a special task. Several threads of execution may be associated with a single process. Thus a process that has only one thread is referred to as a single-threaded process, while a process with multiple threads is referred to as a multi-threaded process.

                  In Java Programming language,  thread is a sequential path of code execution within a program. Each thread has its own local variables, program counter and lifetime. In single threaded runtime environment, operations are executes sequentially i.e. next operation can execute only when the previous one is complete. It exists in a common memory space and can share both data and code of a program. Threading concept is very important in Java through which we can increase the speed of any application. You can see diagram shown below in which a thread is executed along with its several operations with in a single process.

Tuesday, January 18, 2011

Explain what is thread? and it's life cycle in java

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.

When you are programming with threads, understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of several states. By invoking start() method, it doesn’t mean that the thread has access to CPU and start executing straight away. Several factors determine how it will proceed.

Different states of a thread are :

  1. New state – After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.
  2. Runnable (Ready-to-run) state – A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on the processor.
  3. Running state – A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.
  4. Dead state – A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.
  5. Blocked - A thread can enter in this state because of waiting the resources that are hold by another thread.

Saturday, January 15, 2011

Difference between an interface and an abstract class?

 An abstract class may contain code in method bodies, which is not allowed in an interface.
With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

Wednesday, January 12, 2011

sloution to ANT build error "error during FTP transfer: j Connection refused: connect"

This is my personal experience, when I am tying to  access the Unix server through FTP, I got the below error:

C:\ant_scripting\apache-ant-1.8.2\build.xml:20: Could not create type ftp due to java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPClientConfig at$LanguageCode.getValidLanguageCodes(
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
        at java.lang.reflect.Constructor.newInstance(

solution which I have find to avoid this error was given below:

1. To run FTP command  by using ant script, you must have to down load the commons-net-1.4.1.jar file.
  click here to down load the file, please make sure to download the jar file according to your JAVA version,
Find more details on the Apache  link.
2. please make sure to put the jar file in the class path. and run the script, oops you won't get any error now.

please put your comment in case of any issues.            

Wednesday, November 10, 2010

String, StringBuffer & stringBuilder in JAVA

1] Differnces between the above three is:
   String is immutable
   StringBuffer & StringBuilders are mutable.

2] Differnces between the StringBuffer& stringBuiler:
   StringBuffer is synchronized.
   StringBuilder is unsynchronized.

3] On which scenario we have to use the above three?
   String: If the text is not going to change use a string Class because a String object is    immutable.
   StringBuffer: If the text can changes, and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.
   StringBuilder : If the text can change and will only be accessed from a single thread, then use StringBuilder because StringBuilder is unsynchronized.