I'm quite new to the concepts of threads in JAVA and though I tried a couple of codes and they are working I really don't exactly understand whats happening in the background. For example I wrote this piece of code:
public class myThreadTest implements Runnable {
private static void ping(String text, int count)
throws InterruptedException {
for (int i = 0; i<count; i++) {
System.out.println("ping "+text+i+"...");
Thread.sleep(1000);
}
}
public void run() {
try {
ping("run ",10);
} catch (InterruptedException e) {
}
}
public static void main(String[] args) {
(new Thread(new myThreadTest())).start();
try {
ping("main ", 5);
} catch (InterruptedException e) {
}
}
}
are there 2 threads being executed here one running from main and the other from the method run? Bcoz the output I get is main,run,main,run,run,main... something like that.