我對JAVA中線程的概念很陌生,雖然我嘗試了幾個代碼,但他們都在工作,我真的不完全明白在後臺發生了什麼。例如,我寫了這段代碼:瞭解java中的線程
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) {
}
}
}
是否有2個線程在這裏執行一個從main運行,另一個從方法運行? Bcoz我得到的輸出是主要的,運行的,主要的,跑步的,跑步的,主要的......類似的東西。