java多线程测试规定时间内方法执行的次数

package com.zhicheng;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Tread01 extends Thread {
    long start1 = System.currentTimeMillis();
    int count1 = 0;
    
    public void fun1() {
        for(int i = 0 ; i < 1000000; i++) {
            Math.random();
        }
    }

    public void run() {
        
        
        System.out.println("start1"+start1);

        
        while(true) {
            long end1 = System.currentTimeMillis();
            fun1();
            count1++;
            if ((end1 - start1) > 3000) {
                System.out.println("end1"+end1+"差值:"+(end1-start1));            
                System.out.println("count1:"+count1);
                break;
            }
            //Thread.sleep(5000);
        }
    }


}

class Tread02 extends Thread {
    int count2 = 0;
    long start2 = System.currentTimeMillis();
    
    public void fun2(){
        for(int i = 0 ; i < 10000; i++) {
            Math.random();
        }
        
    }
    public void run() {
        System.out.println("start2"+start2);
        while(true) {
            fun2();
            count2++;
            long end2 = System.currentTimeMillis();
            if ((end2 - start2) > 3000 ) {
                System.out.println("end2"+end2+"差值:"+(end2-start2));
                System.out.println("count2:"+count2);
                break;
            }
            //Thread.sleep(7000);
        }
        /*try {
            for (int i = 0; i < 60; i++) {
                Thread.sleep(1000);
                System.out.println("Thread02执行");
            }
            Thread.sleep(1000);
            System.out.println("Thread02执行");
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
    }
    

}

public class MyThread01 {

    public static void main(String[] args) {
        
        Tread01 tread01 = new Tread01();
        Tread02 tread02 = new Tread02();
        tread01.start();
        tread02.start();
        /*long start = System.currentTimeMillis();
        System.out.println("当前时间" + "  " + start);
        ExecutorService executor = Executors.newFixedThreadPool(2);

        executor.execute(new Tread01());
        executor.execute(new Tread02());

        executor.shutdown();
        while (!executor.isTerminated()) {

        }
        ;

        long end = System.currentTimeMillis();
        System.out.println("\n当前时间" + "  " + end);
        System.out.println("\n用时" + "  " + (end - start) / 1000 + "秒");*/
        
    }
}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值