模拟1000个线程 每个线程插入1万条数据到redis

package MasterWorker;


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");
    }
}

猜你喜欢

转载自blog.csdn.net/rdhj5566/article/details/68923069