Friday 11 November 2016

Implementing Runnable Interface



    Runnableinterface declare the run() method that is required for implementing threads in a program. To do this, the following steps have to be followed:
o   Declare the class as implementing the Runnable interface.
o   Implement the run()method.
o   Create a thread by defining an object that is instantiated from this “Runnable” class as the target of the thread.
o   Call the thread’s start()method to run thread.
    Example:
Class A implements Runnable
{
public void run()
 {
for(int i=1;i<=3;i++)
System.out.println("Thread A executing");
 }
}
classThreadrun
{
public static void main(String args[])
 {
  A a1= new A();
  Thread t=new Thread(a1);
t.start();
 }
}
Output:
Thread A executing
Thread A executing
Thread A executing

No comments:
Write comments