Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Java

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Creating a Java Thread by implementing Runnable Interface

By Sanskruti Sanskruti is offline

On 7th May, 2007
Cool Creating a Java Thread by implementing Runnable Interface

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Creating a Java Thread by implementing Runnable Interface link

Author

Sanskruti ( Ambitious contributor )

Yet to provide details about himself


All articles By Sanskruti

Recent Articles

Similar Articles

We can create a thread in Java by extending Thread Class. 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: Java
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: Java
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
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 1 (0 members and 1 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Multithreading in COM subhasish MFC / Win32 3 05-26-2009 11:34 AM
'Main' Thread in Java Sanskruti Java 4 02-18-2009 08:47 AM
Creating a Java Thread by extending Thread Class Sanskruti Java 0 05-06-2007 10:46 PM
Java Thread Model Sanskruti Java 0 05-06-2007 06:58 PM
Worker Threads in MFC Sanskruti MFC / Win32 0 02-28-2007 07:26 PM

 

All times are GMT +5.5. The time now is 05:10 AM.