模拟1000个线程 每个线程插入1万条数据到redis
来源:互联网 发布:淘宝商家如何申请直播 编辑:程序博客网 时间:2024/05/22 15:35
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis操作接口
*/
public class RedisAPI {
private static JedisPool pool = null;
/**
* 构建redis连接池
*
* @param ip
* @param port
* @return JedisPool
*/
public static JedisPool getPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
/* MaxActive:可用连接实例的最大数目,为负数的时候没有限制。
MaxIdle:空闲连接实例的最大数目,为负值时没有限制。
MaxWait:等待获取链接的超时时间。*/
//控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
config.setMaxTotal(-1);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。
config.setMaxIdle(-1);
//表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException;
config.setMaxWaitMillis(1000 * 100);
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
config.setTestOnBorrow(true);
pool = new JedisPool(config, "192.125.155.249", 6379);
}
return pool;
}
/**
* 返还到连接池
*
* @param pool
* @param redis
*/
public static void returnResource(JedisPool pool, Jedis redis) {
if (redis != null) {
pool.returnResource(redis);
}
}
/**
* 获取数据
*
* @param key
* @return
*/
public static String get(String key){
String value = null;
JedisPool pool = null;
Jedis jedis = null;
try {
pool = getPool();
jedis = pool.getResource();
value = jedis.get(key);
} catch (Exception e) {
//释放redis对象
pool.returnBrokenResource(jedis);
e.printStackTrace();
} finally {
//返还到连接池
returnResource(pool, jedis);
}
return value;
}
}
package MasterWorker;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import redis.clients.jedis.Jedis;
public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(1000);
// ExecutorService threadPool = Executors.newCachedThreadPool();
// ExecutorService threadPool = Executors.newSingleThreadExecutor();
long s = System.currentTimeMillis();
//模拟1000个线程 与个线程插入1万条数据
for(int i=1000;i<=1;i++){
final int task = i;
threadPool.execute(new Runnable(){
Jedis jedis = RedisAPI.getPool().getResource();
@Override
public void run() {
for(int j=0;j<=10000;j++){
jedis.set("meters:no:"+task+j, ""+j);
System.out.println("meters:no:"+task+j);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);
}
}
});
}
threadPool.shutdown();
long e = System.currentTimeMillis();
System.out.println("花费时间:"+(e-s)/1000);
System.out.println("all of 10 tasks have committed! ");
}
}
==========
package MasterWorker;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import redis.clients.jedis.Jedis;
public class CountDownLatchTest {
// 模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。
public static void main(String[] args) throws InterruptedException {
long s = System.currentTimeMillis();
// 开始的倒数锁 开始计数
final CountDownLatch begin = new CountDownLatch(1);
// 结束的倒数锁
final CountDownLatch end = new CountDownLatch(500);
// 十名选手
final ExecutorService exec = Executors.newFixedThreadPool(1000);
for (int index = 0; index < 500; index++) {
final int NO = index + 1;
Runnable run = new Runnable() {
Jedis jedis = RedisAPI.getPool().getResource();
public void run() {
try {
// 如果当前计数为零,则此方法立即返回。
// 等待
for(int j=0;j<=2000;j++){
jedis.set("meters:no:"+NO+j, ""+j);
//System.out.println("meters:no:"+task+j);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);
}
//System.out.println("meters:no:"+task+j);
begin.await();
// Thread.sleep((long) (Math.random() * 10000));
System.out.println("No." + NO + " arrived");
} catch (InterruptedException e) {
} finally {
// 每个选手到达终点时,end就减一
end.countDown();
}
}
};
exec.submit(run);
}
System.out.println(" Start");
// begin减一,开始游戏
begin.countDown();
// 等待end变为0,即所有选手到达终点
end.await();
System.out.println(" Over");
exec.shutdown();
long e = System.currentTimeMillis();
System.out.println("花费时间:"+(e-s)/1000+" s");
}
}
- 模拟1000个线程 每个线程插入1万条数据到redis
- 如何解决1万个并发连接,用每个客户一个线程的方法
- 插入10万条数据
- 绝对干货,教你4分钟插入1000万条数据到mysql数据库表
- 4分钟插入1000万条数据到mysql数据库表
- sql 循环插入20万条数据
- Mybatis 批量插入万条数据 !
- sql 语句 循环插入100万条数据到某个表中
- Google多线程面试题: 4个线程向4个文件里写入数据, 每个线程只能写一个值
- Google多线程面试题: 4个线程向4个文件里写入数据, 每个线程只能写一个值
- 导出100万条数据到excel
- 50万条数据保存到数据库
- java让2个线程交替执行,每个线程执行1秒
- 用一个线程模拟多个线程,减少线程数量~
- 用一个线程模拟多个线程,减少线程数量~
- cuda 1000个数据 开256 线程
- 测试2000线程并发下同时查询1000万条数据库表及索引优化
- SQL 执行插入1000万条数据操作 几十秒内!
- ViewPager图片无限自动轮播
- ubuntu安装之后的几件事
- 信号中断 与 慢系统调用
- 加密流程图
- AT&T加入Linux基金会成为白金会员
- 模拟1000个线程 每个线程插入1万条数据到redis
- hdu1085_Holding Bin-Laden Captive!
- android canvas画图、涂鸦断续
- grails 加入ueditor 不显示
- 二叉树遍历,统计
- 3.31 bzoj1086 [SCOI2005]王室联邦
- 【博客分享】优秀的有趣的博客
- postgres大版本升级
- JS中的showModalDialog()方法