并发memcached incr方法调用导致cpu100%

时间:2017-06-27 09:18:11

标签: memcached

@Test
public void concurrency() throws IOException{
    Stopwatch watch = Stopwatch.createStarted();
    MemcachedClientBuilder builder = new XMemcachedClientBuilder("127.0.0.1:11211");
    builder.setFailureMode(true);
    builder.setConnectionPoolSize(10);
    final MemcachedClient client = builder.build();
    final String key = "test";
    final Set<String> contains = Sets.newHashSet(); 
    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 10; i++) {
        pool.execute(new Runnable() {
            @Override
            public void run() {
                for(int k=0;k<999;k++){
                    try {
                        long incr = client.incr(key, 1, 1);
                        System.out.println(incr);
                        contains.add(Long.toString(incr));
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                }
            }
            });
    }
    pool.shutdown();
    while (!pool.isTerminated()) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    watch.stop();
    System.out.println("time:"+watch+",size="+contains.size());
}

// ============================================= ====================

有我的代码,当我运行它时,我的cpu高达100%。

我想在我的项目中使用memcached incr方法。

任何人都可以给我一个帮助吗?谢谢!

对不起我的英语很差。

1 个答案:

答案 0 :(得分:0)

我终于明白了原因。

因为我的jdk版本太低了。

only jdk1.6,where the NIO has a bug.

当我转向jdk1.8时,它运行正常。