What is a thread?
There are
two ways to create a thread:
- By
extending Thread class
- By
implementing Runnable interface.
Thread class provide
constructors and methods to create and perform operations on a thread .Thread
class extends Object class and implements Runnable interface.
Creating a thread
Thread t = new Thread();
By
extending Thread class
The only disadvantage of
this way is you can only inherit one class .
Class Downloder extends
Threds{
Public void run(){ //
always the executing code should be inside the run method
Filedownloder fd= new
Filedownloder();
Fd.downlode();
}
}
To invoke the run method
we should start the thread
Thread t = new Thread();
t.start(); we should call
the start method in order to call the run()
By implementing Runnable interface.
Here the problem of
inheritance is overcome.
Class downloader implement
runnable{
}
Public void run(){
Filedownloder fd =new
Filedownloder(){
Fd.downloder();
}
}
Invoking the run method
downloder d= new
downloader();
Thread t=new Thread(d);
t.start();