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

concurrency memcached incr method invoke lead to cpu100%

@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 方法。

谁能帮帮我?谢谢!

对不起我的英语不好。

终于知道原因了。

因为我的jdk版本太低

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

当我转向 jdk1.8 时,运行 很好。