@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方法。
任何人都可以给我一个帮助吗?谢谢!
对不起我的英语很差。