How Many Ways can you Create a Threads in Java?

Threads  are  implemented  in  the  form of  objects  that contain method  called  run(). The run() method is the heart and soul of any thread. It make up the entire body of a thread and is the only method in which the threads behavior can be implemented. A typical run()  appear as follows :
                                                                 
public void run()
{
-----------   //statements for implementing
-----------      threads
}

The run() method should be invoked by an object of the concerned  thread and initiating it with the help of anther thread  method called start().

A new thread can be created in two ways:

1. By creating a thread class :


define a class that extends thread class and override its run () method with the code recognized by the thread.

2.By converting class to a thread :

We can make our class runnable as thread by extending class java.lang.thread this gives us access to all the thread methods directly.it includes the following steps.

    1.      Declare the class as extending the thread class imp
    
    2.      The run()method that is responsible for executing the sequence of code that thread will execute.
    
    3.      Create a thread object and call the start()method to initiate the thread execution.

DECLARING THE CLASS:

The thread can be extended as follows:

class A extends Thread
{
------
------
}

Now we have a new type of thread my thread.

IMPLEMENTING THE RUN METHOD:

The run ( ) method has been inherited by the class my thread. We   have to over ride this method in order to implement the code to be executed by our thread. The basic implementation of  run( ) will look like this:

public void run( )
{
  -----
  -----
  -----
 }

When we start the new thread, java calls the threads run ( )where all the action takes place.

STARTING NEW THREAD:

To actually create and run instance of our thread class, we must write the following.

MyThread athread   = new MyThread ( );
athread .start ( );

The first line instantiates a new object of my class my thread. Note that this statement just creates the object. The thread that will run the object is not yet running. The thread is in a new born state.

The second line calls the start ( ) method causing the thread to move into the run able state.

Then, the java run time will schedule the thread to run by invoking its run ( ) method. Now, the thread is said to be in the running state.

Example:

class A extends Thread
{
   public void run ( )
    {
       for ( int i=1; i < 5; i++ )
       {
          System.out.println  (\t from threadA: i=+i);
        }
     System out println(exit from A);
    }
}

class B extends Thread
{
public void run ( )
{
for (int i=1;j<=5;j++)
{
System.out. println (\t from thread B:j= +j);
}
System.out.println (exit from B);
}
}

class C extends Thread
{
public void run( )
{
for(int k=1;k <= 5;k++)
{
System.out.println (\t from thread c:k=+k);
}
System.out.println(exit from c);
}
}

class Threadtest
{
public static void main( String args[ ])
{
new A( ).Start( );
new B( ).Start( );
new C( ).Start( );
}
}

Implementing  the runnable interface:

We can create threads in two ways, One by using the extended thread class and another by implementing the runnable interface.

How to make use of the runnable interface to implement threads:

The runnable interface declares the run( )method that is required for implementing threads in our programs.To do this,We must perform the steps listed below.

1.Declare the class as implementing the runnable interface.

2.Implement the run( )method.

3.Create a thread by defining an object that is instantiated from this runnable class on the target of the thread.

4.Call the threads start( )method to run the thread.   

Example:

class X implements Runnable
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println(\t ThreadX+i);
}
System.out.println(End of thread X);
}
}

class RunnableTest
{
public static void main (String args[ ])
{
X runnable =new X( );
Thread threadX=new Thread(runnable);
threadX.Start( );
System.out.println(End of main Thread);
}
}



Related

Java 7162012074200762665

Post a Comment

emo-but-icon

item