Thread using Runnable Interface



 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
29
30
31
32
33
34
class RunnableThread implements Runnable{
 
    public static int myCount = 0;
    public RunnableThread(){
         
    }
    public void run() {
        while(RunnableThread.myCount <= 10){
            try{
                System.out.println(" Thread: "+(++RunnableThread.myCount));
                Thread.sleep(100);
            } catch (InterruptedException iex) {
                System.out.println("Exception in thread: "+iex.getMessage());
            }
        }
    }
}
public class RunnableExample {
    public static void main(String a[]){
        System.out.println("Main Thread Started");
        RunnableThread mrt = new RunnableThread();
        Thread t = new Thread(mrt);
        t.start();
        while(RunnableThread.myCount <= 10){
            try{
                System.out.println("Main Thread: "+(++RunnableThread.myCount));
                Thread.sleep(100);
            } catch (InterruptedException iex){
                System.out.println("Exception in main thread: "+iex.getMessage());
            }
        }
        System.out.println("Main Thread End");
    }
}