package com.myjava.threads;
class MyRunnableThread implements Runnable{
public static int myCount = 0;
public MyRunnableThread(){
}
public void run() {
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Expl Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
}
public class RunMyThread {
public static void main(String a[]){
System.out.println("Starting Main Thread...");
MyRunnableThread mrt = new MyRunnableThread();
Thread t = new Thread(mrt);
t.start();
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Main Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex){
System.out.println("Exception in main thread: "+iex.getMessage());
}
}
System.out.println("End of Main Thread...");
}
}
class MyRunnableThread implements Runnable{
public static int myCount = 0;
public MyRunnableThread(){
}
public void run() {
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Expl Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex) {
System.out.println("Exception in thread: "+iex.getMessage());
}
}
}
}
public class RunMyThread {
public static void main(String a[]){
System.out.println("Starting Main Thread...");
MyRunnableThread mrt = new MyRunnableThread();
Thread t = new Thread(mrt);
t.start();
while(MyRunnableThread.myCount <= 10){
try{
System.out.println("Main Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
} catch (InterruptedException iex){
System.out.println("Exception in main thread: "+iex.getMessage());
}
}
System.out.println("End of Main Thread...");
}
}
Example Output
Starting Main Thread... Main Thread: 1 Expl Thread: 2 Main Thread: 3 Expl Thread: 4 Main Thread: 5 Expl Thread: 6 Main Thread: 7 Expl Thread: 8 Main Thread: 9 Expl Thread: 10 Main Thread: 11 End of Main Thread...
No comments:
Post a Comment