1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Creating a Java Thread by implementing Runnable Interface

Discussion in 'Java' started by Sanskruti, May 7, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    We can [thread=4183]create a thread in Java by extending Thread Class[/thread]. A better way to create a thread in Java is to implement Runnable interface. A thread can be created by extending Java Thread class also. Now the question arises why implementing Runnable interface is a better approach? Answer is, if the thread class you are creating is to be subclass of some other class, it can’t extend from the Thread class. This is because Java does not allow a class to inherit from more than one class. In such a case one can use Runnable interface to implement threads.

    Let us see an example of creating a thread by implementing Runnable interface.

    Code:
    class PrintString 
    {
    	public static void main (String args [ ]) 
    	{
    		StringThread t = new StringThread ("Java",50);
    		new Thread(t). start ( );
    	}
    }	
    
    class StringThread implements Runnable 
    {
    	private String str;
    	private int num;
    
    	StringThread(String s, int n) 
    	{
    		str  = new String (s);
    		num =n;	
    	}
    	
    	public void run ( ) 
    	{
    		for (int i=1; i<=num; i++)
    			System.out.print (str+" ");	
    	}
    }
    The output is:
    Code:
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java
    If you have had created a thread class by extending Thread class, you could have directly called start() method as t.start (), where t is a thread object. This is because thread class created by extending Thread class is a subclass of Thread class, so it has all functionalities of a thread. While creating a thread implementing Runnable, a Thread object will have to be explicitly created which is what PrintString class is doing. It then passes StringThread object as a parameter to this thread and runs it. This causes the run ( ) method of StringThread class to get executed.

    The same program can be rewritten, by implementing Runnable interface, and creating a Thread object inside StringThread class rather than in the PrintString class as below.
    Code:
    class PrintString 
    {
    	public static void main (String args [] ) 
    	{
    		new StringThread("Java",50 );
    	}
    }
    
    class StringThread implements Runnable 
    {
    	private String str;
    	private int num;
    
    	StringThread(String s, int n) 
    	{
    		str=new String (s);
    		num=n;
    		Thread t=new Thread (this);
    		t. start ( );
    	}
    	
    	public void run () 
    	{
    		for (int i=1; i<=num; i++)
    			System.out.print (str+" ");
    	}
    }
    
    The output is:
    Code:
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
    Java Java
     
  2. TPrice

    TPrice New Member

    Joined:
    Aug 24, 2010
    Messages:
    9
    Likes Received:
    1
    Trophy Points:
    0
    Thank you for this thread. This sure seems simple enough. I have been trying to figure this out, and I can see from this thread that I was going about it in completely the wrong way. I think I got it now, thank goodness. I really appreciate all of the great information that I am finding in this forum.
     
  3. Tobiasgar

    Tobiasgar New Member

    Joined:
    Aug 29, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thanx for the script)) the code is admiring:)
     
  4. lyfzopen

    lyfzopen New Member

    Joined:
    Sep 4, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi dude , your code is good and simple but I HAVE A QUESTION , will there be any difference if i call the StringThread.run method directly ???

    ie


    Code: Java

    Code:
    class PrintString
    {
        public static void main (String args [ ])
        {
            StringThread t = new StringThread ("Java",50);
           // new Thread(t). start ( );   instead of this i do :
            t.run();
        }
    }   
    
    class StringThread implements Runnable
    {
        private String str;
        private int num;
    
        StringThread(String s, int n)
        {
            str  = new String (s);
            num =n; 
        }
       
        public void run ( )
        {
            for (int i=1; i<=num; i++)
                System.out.print (str+" "); 
        }
    }
    If no , why are u doing so ??? please reply asap ! .. :)
     
    Last edited by a moderator: Sep 4, 2011
  5. prabhas4

    prabhas4 New Member

    Joined:
    Feb 11, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    start() method ll call run method... if do so (instead of start() , calling run()) here would be any Error or Exception run() method will simply be executed in the same Thread and new Thread will not be created.

    and also u can't call same start() twice ....
    t. start ( );
    t. start ( ); // IllegalThreadStateException............

    Regards
    FOSS
     

Share This Page