Posted By:
Anonymous
Posted On:
Thursday, September 8, 2005 10:30 AM
Here is an example program to show how we can use more than one thread in a java program.
public class ThreadExample extends Thread{// this class is extending thread
public static void main(String args[]){
// this is a main thread
ThreadExample te = new ThreadExample();
te.threadDemo();
te.start();
te.runnableDemo();
}
// when we call start method of the above thread the run method will execute
public void run(){
System.out.println("Hello Sharad");
}
public void threadDemo(){
ThreadExample t = new ThreadExample();
t.start();
}
public void runnableDemo(){
//new thread through runnable
Runnable r = new Runnable(){
public void run(){
System.out.println("hi Sharad");
}
};
r.start(); // it will start new thread
}
}
For more details please refer to the following links
http://www.javaworld.com/javaworld/jw-04-1996/jw-04-threads.html
http://docs.rinet.ru/JWP/ch16.htm
http://www.javaprepare.com/notes/threads.html
you can also practice coding online at
www.devsquare.com