LRU缓存的实现之性能测试

本文介绍了一个使用Java进行的并发性能测试案例,通过50个线程对10,000条随机数据进行读写操作,展示了如何设置线程池并记录性能指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

针对上一篇文章,这里给出性能测试:10000条随机数据,50个线程读写。

 

package lru;

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

public class Performance{
	static final int loop = 10000;
	static final int threadCount = 50;
	static OjadbMap<Integer, Integer> lruMap = new OjadbMap<Integer, Integer>(loop);
	static ExecutorService exec = Executors.newFixedThreadPool(threadCount);
	static long nn = 0;
	static int check=0;

	public static void main(String[] args) throws InterruptedException {
		Performance p = new Performance();
		p.start();
	}

	private void start() throws InterruptedException {
		loops();
		Thread.sleep(7500);
		System.out.println(check+" spend time=" + nn);
		System.out.println(" every milli-seconds=" + (check / nn));
		lruMap=null;
		exec.shutdown();
	}

	private void loops() {
		for (int i = 0; i < threadCount; i++) {
			exec.execute(new Thread("thread-" + i){
				@Override
				public void run() {
					final long begin = System.currentTimeMillis();
					Random r = new Random();
					for (int j = 0; j < loop; j++) {
						check++;
						int n = r.nextInt(100);
						if (null == lruMap.get(n)) {
							lruMap.put(n, j);
						} else {
						}
					}
					final long end = System.currentTimeMillis();
					final long elapsed = end - begin;
					nn = nn + elapsed;
				}
			});
		}
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值