Java Thread 理解多線程併發編程與順序(串行)編程代碼示例

package unittest;

public class Test extends Thread{
String title;
Test(String title){
this.title = title;
}
public void run(){
for(int i = 0; i < 3; i++){
System.out.println(this.title+ ":hello " + i);
}
}
public static void main(String[] args) {
//直接調用run方法,沒有併發執行,而是順序執行
new Test(“Thread 1”).run();
new Test(“Thread 2”).run();
new Test(“Thread 3”).run();

	/*
	//調用start方法,併發執行,出現交替執行的現象
	new Test("Thread 1").start();
	new Test("Thread 2").start();
	new Test("Thread 3").start();
	*/
	
	throw new RuntimeException("");
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章