标签:
public static void main (String[] args) { String servers = "10.128.7.20:2181,10.128.7.20:2182,10.128.7.20:2183"; CuratorFramework curator = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(10000, 3)).connectString(servers).build(); curator.start(); final InterProcessMutex lock = new InterProcessMutex(curator, "/global_lock"); Executor pool = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i ++) { pool.execute(new Runnable() { public void run() { try { lock.acquire(); System.out.println(Thread.currentThread().getName()); TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); }finally{ try { lock.release(); } catch (Exception e) { e.printStackTrace(); } } } }); } }
标签:
原文地址:http://my.oschina.net/u/2250599/blog/503807