Showing posts with label runnable interface. Show all posts
Showing posts with label runnable interface. Show all posts

Monday, 24 March 2014

Thread using Runnable Interface

Creating Thread using Runnable Interface

The Runnable interface should be implemented by any class whose instances are intended to be executed as a thread.

The class must define run() method of no arguments.

The run() method is like main() for the new thread provides the means for a class to be active while not subclassing thread.

A thread that implements Runnable can run without sub classing thread by instantiating a thread instance and passing itself in as the target.

Program

class AThread implements Runnable
{
public void run()
{
for (int i=1;i<=10;i++)
System.out.println("Iam runnable");
}
}
class RunnableDemo
{
main(0
{
AThread a = new AThread();
Thread t1 = new Thread(a);
t1.start(0;
}
}