编程语言
首页 > 编程语言> > java7-多线程

java7-多线程

作者:互联网

1,创建线程

public class test {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("当前线程为:"+Thread.currentThread().getName());

        //非多线程执行
        long start = System.currentTimeMillis();
        for(int i=0; i<10; i++){
            //System.out.println(""+i+" "+getSum(1, 1));
            getSum(1, 1);
        }
        System.out.println("总耗时" + (System.currentTimeMillis()-start));

        //多线程执行
        start = System.currentTimeMillis();
        for(int i=0; i<10; i++){
            Thread thread = new Thread(()->{
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            thread.start();
        }
        System.out.println("总耗时" + (System.currentTimeMillis()-start));
    }

    static int getSum(int a, int b) throws InterruptedException {
        Thread.sleep(100);
        return a+b;
    }
}

标签:执行,Thread,int,System,start,java7,线程,多线程
来源: https://www.cnblogs.com/tensorzhang/p/16256239.html