1. 程式人生 > >九、curator recipes之不可重入鎖InterProcessSemaphoreMutex

九、curator recipes之不可重入鎖InterProcessSemaphoreMutex

print ces lba art cto catch apache ava har

簡介

recipes的InterProcessSemaphoreMutex是一種不可重入的互斥鎖,也就意味著即使是同一個線程也無法在持有鎖的情況下再次獲得鎖,所以需要註意,不可重入的鎖很容易在一些情況導致死鎖,比如你寫了一個遞歸。

官方文檔:http://curator.apache.org/curator-recipes/shared-lock.html

javaDoc:http://curator.apache.org/curator-recipes/shared-lock.html

代碼示例

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex; import org.apache.curator.retry.ExponentialBackoffRetry; public class SemaphoreMutexDemo { private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new
ExponentialBackoffRetry(3000, 3)); private static String path = "/locks/semaphore/0001"; static { client.start(); } public static void main(String[] args) throws Exception { startThread0(); Thread.sleep(10); startThread1(); Thread.sleep(50000); client.close(); }
private static void startThread0() { new Thread(() -> { InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, path); try { System.out.println("thread0 acquiring"); lock.acquire(); System.out.println("thread0 acquired"); System.out.println("thread0 sleep for 3s"); Thread.sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { try { lock.release(); System.out.println("thread0 release"); } catch (Exception e) { e.printStackTrace(); } } }).start(); } private static void startThread1() { new Thread(() -> { InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, path); try { System.out.println("thread1 acquiring"); lock.acquire(); System.out.println("thread1 acquired"); System.out.println("thread1 sleep for 3s"); Thread.sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { try { lock.release(); System.out.println("thread1 release"); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }

九、curator recipes之不可重入鎖InterProcessSemaphoreMutex