首页 > 编程语言 > 详细

Java-多线程-Thread类

时间:2020-08-24 16:47:47      阅读:64      评论:0      收藏:0      [点我收藏+]

1、常规的单线程

package cn.bruce.Thread;

//如何创建和启动一个线程
//创建Thread子类对象
//子类对象调用方法start()
//让线程程序执行,JVM调用线程中的run
public class MoreThreadDemo {
    public static void main(String[] args) {
        long ss = System.currentTimeMillis();
        SubThread st = new SubThread();
        st.run();// 如果是直接调用run的话还是单线程
        // st.start();
        long s = System.currentTimeMillis();
        long r = fun(45);
        long e = System.currentTimeMillis();
        System.out.println("main..." + (e - s));
        System.out.println(r);
        long ee = System.currentTimeMillis();
        System.out.println("result..." + (ee - ss));
    }

    public static long fun(long a) {
        if (a == 1)
        {
            return 1;
        } else if (a == 2)
        {
            return 1;
        }
        return fun(a - 2) + fun(a - 1);
    }

}

// 定义子类,继承Thread
// 重写方法run
class SubThread extends Thread {
    public void run() {
        long s1 = System.currentTimeMillis();
        long r1 = fun1(45);
        long e1 = System.currentTimeMillis();
        System.out.println("run..." + (e1 - s1));
        System.out.println(r1);
    }

    public static long fun1(long a) {
        if (a == 1)
        {
            return 1;
        } else if (a == 2)
        {
            return 1;
        }
        return fun1(a - 2) + fun1(a - 1);
    }
}

技术分享图片

 

 需要6874ms

2、开辟一个线程后

package cn.bruce.Thread;

//如何创建和启动一个线程
//创建Thread子类对象
//子类对象调用方法start()
//让线程程序执行,JVM调用线程中的run
public class MoreThreadDemo {
    public static void main(String[] args) {
        long ss = System.currentTimeMillis();
        SubThread st = new SubThread();
        // st.run();// 如果是直接调用run的话还是单线程
        st.start();//开辟线程运行
        long s = System.currentTimeMillis();
        long r = fun(45);
        long e = System.currentTimeMillis();
        System.out.println("main..." + (e - s));
        System.out.println(r);
        long ee = System.currentTimeMillis();
        System.out.println("result..." + (ee - ss));
    }

    public static long fun(long a) {
        if (a == 1)
        {
            return 1;
        } else if (a == 2)
        {
            return 1;
        }
        return fun(a - 2) + fun(a - 1);
    }

}

// 定义子类,继承Thread
// 重写方法run
class SubThread extends Thread {
    public void run() {
        long s1 = System.currentTimeMillis();
        long r1 = fun1(45);
        long e1 = System.currentTimeMillis();
        System.out.println("run..." + (e1 - s1));
        System.out.println(r1);
    }

    public static long fun1(long a) {
        if (a == 1)
        {
            return 1;
        } else if (a == 2)
        {
            return 1;
        }
        return fun1(a - 2) + fun1(a - 1);
    }
}

技术分享图片

 

 只需要4042ms了

Java-多线程-Thread类

原文:https://www.cnblogs.com/BruceKing/p/13554459.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!