java线程复习1(线程创建)

当全部的线程执行结束时(更具体点,所有非守护线程结束时),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
",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");
    }
}

结果:

原文地址:https://www.cnblogs.com/da-peng/p/8401570.html