1. 程式人生 > >java線程復習1(線程創建)

java線程復習1(線程創建)

end nts read int 完成 image rri style @override

當全部的線程執行結束時(更具體點,所有非守護線程結束時),Java程序就結束了。如果初始線程(執行main()方法的主線程)運行結束,其他的線程還是會繼續執行直到執行完成。但是如果某個線程調用System.exit()指示終結程序,那麽全部的線程都會結束執行。

Calculator類:

package MyThread.Thst2;

/*
 * Create by dapeng on 2018/2/1
 */
public class Caculator implements Runnable {

    private int number;
    public Caculator(int number){
        
this.number = number; } @Override public void run() { for(int i = 1 ; i < 10 ; i++){ System.out.printf("%s:%d*%d=%d\n",Thread.currentThread().getName(),number,i,i*number); } } }

Main函數

package MyThread.Thst2;

/*
 * Create by dapeng on 2018/2/1
 */
public class
Main { public static void main(String[] args) { for(int i = 0 ; i < 10 ; i++){ Caculator caculator = new Caculator(i); Thread thread = new Thread(caculator); thread.start(); } System.out.println("end main"); } }

結果:

技術分享圖片

java線程復習1(線程創建)