by Dinesh Thakur Category: Multithreading

Another method to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. We can construct a thread on any object that implements Runnable. To implement Runnable, a class need only implement a single method called run().

Syntax:

public void run()

Inside run(), we define the code that constitutes the new thread. run() can call other methods, use other classes, and declare variables, just like the main thread can. The only difference is that run() establishes the entry point for another, concurrent thread of execution within the program. This thread will end when run() returns.

After we create a class that implements Runnable, we will instantiate an object of type Thread from within that class with the help of following constructor:

Thread(Runnable obj, String tname)

In this constructor, obj is an instance of a class that implements the Runnable interface. This defines where execution of the thread begins. The name of the new thread is specified by tname.

After the new thread is created, it will not start running until we call its start() method, which is declared within Thread. It is the start() method which invokes the run() method.

class ImplementingRunnableThread implements Runnable

       {
           Thread t;
           int i;
           String s[]={"Welcome","to","Java","Programming","Language"};
           ImplementingRunnableThread()
             {
             t=new Thread(this,"Runnable Interface Thread");
             System.out.println("Thread is:"+t);
             t.start();
             }
                 public void run()
                {
                        String name=t.getName();
                    for(int i=0;i<s.length;++i)
                      {
                          try
                           {
                          t.sleep(500);
                           }
                          catch(Exception e)
                              {
                              }
                              System.out.println(name+":"+s[i]);
                       }
                    }
        }
                                   class RunnableInterfaceExample
                                       {
                                         public static void main(String args[])
                                           {
                                           new ImplementingRunnableThread();
                                          }
                                       }

Implementing Runnable Interface in Java Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article