| Previous | Home | Next |
Using Thread class a basic thread program is given as follows.
/*
/*
* save as threddemo.java
*Shows imlementation of Thread usin gThread class
*/
package multithradingexample1;
class thread_imp extends Thread{
private String name;
thread_imp(String name)
{
super("Demo Thread");
this.name=name;
start();
}
public void run()
{
for(int i=0;i<6;i++)
{
try{
System.out.println(name);
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.print(e.toString());
}
}
}
}
public class thraddemo {
public static void main(String[] Str)
{
thread_imp th1 = new thread_imp("Thread1");
thread_imp th2 = new thread_imp("Thread2");
}
}
Output:
Thread1 Thread2 Thread1 Thread2 Thread1 Thread2 Thread2 Thread1 Thread1 Thread2 Thread1 Thread2
| Previous | Home | Next |