溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Curator教程(三)分布式鎖

發(fā)布時間:2020-07-25 00:17:17 來源:網(wǎng)絡(luò) 閱讀:1258 作者:布拉君君 欄目:大數(shù)據(jù)
共享鎖
@Testpublic void sharedLock() throws Exception {    // 創(chuàng)建共享鎖
    InterProcessLock lock = new InterProcessSemaphoreMutex(client, lockPath);    // lock2 用于模擬其他客戶端
    InterProcessLock lock2 = new InterProcessSemaphoreMutex(client2, lockPath);    // 獲取鎖對象
    lock.acquire();    // 測試是否可以重入
    // 超時獲取鎖對象(第一個參數(shù)為時間, 第二個參數(shù)為時間單位), 因為鎖已經(jīng)被獲取, 所以返回 false
    Assert.assertFalse(lock.acquire(2, TimeUnit.SECONDS));    // 釋放鎖
    lock.release();    // lock2 嘗試獲取鎖成功, 因為鎖已經(jīng)被釋放
    Assert.assertTrue(lock2.acquire(2, TimeUnit.SECONDS));
    lock2.release();
}
共享可重入鎖
public void sharedReentrantLock() throws Exception {    // 創(chuàng)建可重入鎖
    InterProcessLock lock = new InterProcessMutex(client, lockPath);    // lock2 用于模擬其他客戶端
    InterProcessLock lock2 = new InterProcessMutex(client2, lockPath);    // lock 獲取鎖
    lock.acquire();    try {        // lock 第二次獲取鎖
        lock.acquire();        try {            // lock2 超時獲取鎖, 因為鎖已經(jīng)被 lock 客戶端占用, 所以獲取失敗, 需要等 lock 釋放
            Assert.assertFalse(lock2.acquire(2, TimeUnit.SECONDS));
        } finally {
            lock.release();
        }
    } finally {        // 重入鎖獲取與釋放需要一一對應(yīng), 如果獲取 2 次, 釋放 1 次, 那么該鎖依然是被占用, 如果將下面這行代碼注釋, 那么會發(fā)現(xiàn)下面的 lock2 獲取鎖失敗
        lock.release();
    }    // 在 lock 釋放后, lock2 能夠獲取鎖
    Assert.assertTrue(lock2.acquire(2, TimeUnit.SECONDS));
    lock2.release();
}
共享可重入讀寫鎖
@Testpublic void sharedReentrantReadWriteLock() throws Exception {    // 創(chuàng)建讀寫鎖對象, Curator 以公平鎖的方式進(jìn)行實現(xiàn)
    InterProce***eadWriteLock lock = new InterProce***eadWriteLock(client, lockPath);    // lock2 用于模擬其他客戶端
    InterProce***eadWriteLock lock2 = new InterProce***eadWriteLock(client2, lockPath);    // 使用 lock 模擬讀操作
    // 使用 lock2 模擬寫操作
    // 獲取讀鎖(使用 InterProcessMutex 實現(xiàn), 所以是可以重入的)
    InterProcessLock readLock = lock.readLock();    // 獲取寫鎖(使用 InterProcessMutex 實現(xiàn), 所以是可以重入的)
    InterProcessLock writeLock = lock2.writeLock();    /**
     * 讀寫鎖測試對象
     */
    class ReadWriteLockTest {        // 測試數(shù)據(jù)變更字段
        private Integer testData = 0;        private Set<Thread> threadSet = new HashSet<>();        // 寫入數(shù)據(jù)
        private void write() throws Exception {
            writeLock.acquire();            try {
                Thread.sleep(10);
                testData++;
                System.out.println("寫入數(shù)據(jù) \ t" + testData);
            } finally {
                writeLock.release();
            }
        }        // 讀取數(shù)據(jù)
        private void read() throws Exception {
            readLock.acquire();            try {
                Thread.sleep(10);
                System.out.println("讀取數(shù)據(jù) \ t" + testData);
            } finally {
                readLock.release();
            }
        }        // 等待線程結(jié)束, 防止 test 方法調(diào)用完成后, 當(dāng)前線程直接退出, 導(dǎo)致控制臺無法輸出信息
        public void waitThread() throws InterruptedException {            for (Thread thread : threadSet) {
                thread.join();
            }
        }        // 創(chuàng)建線程方法
        private void createThread(int type) {
            Thread thread = new Thread(new Runnable() {                @Override
                public void run() {                    try {                        if (type == 1) {
                            write();
                        } else {
                            read();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            threadSet.add(thread);
            thread.start();
        }        // 測試方法
        public void test() {            for (int i = 0; i < 5; i++) {
                createThread(1);
            }            for (int i = 0; i < 5; i++) {
                createThread(2);
            }
        }
    }

    ReadWriteLockTest readWriteLockTest = new ReadWriteLockTest();
    readWriteLockTest.test();
    readWriteLockTest.waitThread();
}

測試結(jié)果如下:

寫入數(shù)據(jù) 1
寫入數(shù)據(jù) 2
讀取數(shù)據(jù) 2
寫入數(shù)據(jù) 3
讀取數(shù)據(jù) 3
寫入數(shù)據(jù) 4
讀取數(shù)據(jù) 4
讀取數(shù)據(jù) 4
寫入數(shù)據(jù) 5
讀取數(shù)據(jù) 5


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI