Create Thread using Thread class



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class HelloThread extends Thread{
    public static int myCount = 0;
    public void run(){
        while(HelloThread.myCount <= 10){
            try{
                System.out.println("Thread: "+(++HelloThread.myCount));
                Thread.sleep(100);
            } catch (InterruptedException iex) {
                System.out.println("Exception in thread: "+iex.getMessage());
            }
        }
    }
}
public class MainClass {
    public static void main(String a[]){
        
        HelloThread mst = new HelloThread();
        mst.start();
        while(HelloThread.myCount <= 10){
            try{
                System.out.println("Main Thread: "+(++HelloThread.myCount));
                Thread.sleep(100);
            } catch (InterruptedException iex){
                System.out.println("Exception in main thread: "+iex.getMessage());
            }
        }        
    }
}