Friday, August 31, 2012

Thread Concept Part-I


Thread

All java programs are run under a thread in JVM. Java program runs in default thread called “main” thread.  Java main method is invoked the JVM start ‘main’ thread to run the program. Java has capability to run multiple threads for application. But multi-threading is the most unpredicted behavior for any java application at runtime.

Every thread is run in different stack in JVM. Multi-threading is talking about to run multiple thread at a time to perform multiple operations. System processor schedules the time frame to run the thread one by one. All threads are come in a queue use processor time to execute.

There several procedure to prioritize the threads in queue. Mainly “Round-Robin” theory is applied for allocating process time. Set priority is another procedure to schedule the queue for processor execution sequence. But there is no guaranty thread will be executed with applied theories and schedule. Thread execution behavior will be different for different systems, different operating systems and different infrastructure.

Thread Creation

There are two ways to create thread in java. Java provides ‘Runnable’ interface and ‘Thread’ class for thread programming.

1.       Extends ‘Thread’ class.

a.       Extends java.lang.Thread class in your thread class

b.     
 
Override run() method for operation.


public class FirstMyThread1 extends Thread{

   public void run() {

                  System.out.println(" New thread started. :"

                     + Thread.currentThread().getName());

         System.out.println(" Add operation code here for new thread.");

   }

}

2.       Implements ‘Runnable’ interface

a.       Implement java.lang.Runnable interface in your class.

b.     
 
Implements run() method for operation.


public class FirstMyThread2 implements Runnable{

   @Override

   public void run() {

                  System.out.println(" New thread started. :"

                     + Thread.currentThread().getName());

         System.out.println(" Add operation code here for new thread.");

   }

}

 

 

 

Thread Start

Create Thread class object and call the start() method to run the new thread.

1.       Extends ‘Thread’ class.

MyThread1 mThread1=new MyThread1();

mThread1.start();

 

2.       Implements ‘Runnable’ interface

MyThread2 mThread2=new MyThread2();         

Thread t1=new Thread(mThread2);

t1.start();

 

Points need to remember –

i)        start() method is in jav.lang.Thread class

ii)       As your thread class extends java.lang.Thread class then you can call start() method directly.

iii)     You implements java.lang.Runnable interface to create my thread class

a.       You only override run() method

b.      start() method is not available in your thread class.

c.       So pass your thread class in java.lang.Thread class to assign thread task.

d.      In this case your thread class only define thread job

e.      Here java.lang.Thread class is a worker class to execute you job

iv)     mThread1.run() or mThread2.run() are valid method call

a.       If you call mThread1.run() then new thread will not start.

b.      This method will execute as part of current thread.

c.       No exception will thrown

v)      If you start more than one thread there is no guaranty to start those threads in your order you started.

vi)     All thread will be started from main thread.

 


 

Thread Example#1

§  Create thread class extending java.lang.Thread class (file name: FirstMyThread1.java)

public class FirstMyThread1 extends Thread {

   public void run() {

         System.out.println(" New thread started. :"

                     + Thread.currentThread().getName());

         System.out.println(" Add operation code here for new thread.");

   }

}

§  Create thread class implementing java.lang.Runnable interface (file name: FirstMyThread2.java)

public class FirstMyThread2 implements Runnable {

   @Override

   public void run() {

         System.out.println(" New thread started. :"

                     + Thread.currentThread().getName());

         System.out.println(" Add operation code here for new thread.");

   }

 

}

§  Create Main method class (file name: FirstMyThreadMain.java)

public class FirstMyThreadMain {

   public static void main(String[] args) {

         FirstMyThread1 fmThread1=new FirstMyThread1();

         fmThread1.start();

        

         FirstMyThread2 fmThread2=new FirstMyThread2();

         Thread t=new Thread(fmThread2);

         t.start();

   }

}

§  Run and see output

New thread started. :Thread-0

New thread started. :Thread-1

Add operation code here for new thread.

Add operation code here for new thread.

 

1 comment: