Wednesday, April 6, 2011

A Sample Multithreading Program


Thread can be created 2 ways.
1. our thread name should be extends Thread
2. our thread name should be implements Runnable Interface

here the following program 2 threads are there.Mythread1,Mythread2 respectively..these two's are extends Thread.Thread is born when the our thread is created when the start() method calling on the particural thread object..

class Mythread1 extends Thread{
public void run(){
for(int i=1;i<=30;i++)

System.out.println(i);

}
}
class Mythread2 extends Thread{
public void run(){
for(int i=31;i<=50;i++)
System.out.println(i);
}
}
class MythreadTest{
public static void main(String args[]) throws Exception {
Mythread1 t1=new Mythread1();
Mythread2 t2=new Mythread2();
t2.start();
t1.start();

}
}

1 comment:

  1. Hello Admin,

    here the following program 2 threads are there.

    Regards
    Thanks

    Mansi Sharma

    ReplyDelete