多线程测试用例

package com.zhaowd.test.designPattern.singleton;

import java.util.concurrent.CountDownLatch;

import com.zhaowd.test.designPattern.singleton.laze.LazeTwo;

public class Test {
	public static void main(String[] args) {
        int count = 2000;
        
        final CountDownLatch latch = new CountDownLatch(count);
        long time = System.currentTimeMillis();
        for(int i = 0;i < count; i ++){
                
            new Thread(){
                
                @Override
                public void run(){
                    
                    //所有线程执行到这里进行阻塞,count=0时,所有线程从该位置同时触发执行
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                    //饿汉式:在使用前进行初始化,线程安全
//                    System.out.println(System.currentTimeMillis() + ":"  + Hungry.getInstance());
                    
                    //懒汉式:线程不安全,需要使用synchronized关键字(会较低效率)
//                    System.out.println(System.currentTimeMillis() + ":"  + LazeOne.getInstance());
                    
                    //懒汉式-内部类/代码块:线程安全的;注意:使用静态内部类注意加载顺序,只有用到才会加载内部类
                    System.out.println(System.currentTimeMillis() + ":"  + LazeTwo.getInstance());
                    
                }
            }.start();
            
            latch.countDown();
        }
        
        System.out.println("总耗时:"+ (System.currentTimeMillis() - time));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值