- public class TestThread1 {
- public static void main(String[] args) {
- Runner1 r = new Runner1();
- // r.run();//這是方法調用,而不是開啓一個線程
- Thread t = new Thread(r);// 調用了Thread(Runnable target)方法。且父類對象變量指向子類對象。
- t.start();
- for (int i = 0; i < 100000; i++) {
- if(i%10000==0){
- System.out.println("main");
- }
- }
- }
- /*
- * run的運行結果:
- * 先顯示10個thread,再來上10個main,
- * 表示使用run方法調用的時候相當於一般的過程調用,並沒有加入到線程中區
- * start的運行結果:
- * thread和main是穿插進行的,就表示該方法已經加入了線程中去
- */
- }
- class Runner1 extends Thread {
- public void run() {
- for (int i = 0; i < 100000; i++) {
- if(i%10000==0){
- System.out.println("thread");
- }
- }
- }
- }
java 線程的run和start方法的不同
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.