Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Curator tutorial (3) distributed Lock

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Shared lock @ Testpublic void sharedLock () throws Exception {/ / create shared lock InterProcessLock lock = new InterProcessSemaphoreMutex (client, lockPath); / / lock2 is used to impersonate other clients InterProcessLock lock2 = new InterProcessSemaphoreMutex (client2, lockPath); / / acquire lock object lock.acquire () / / Test whether the lock object can be reentered / / timeout acquired (the first parameter is time, the second parameter is time unit). Since the lock has been acquired, false Assert.assertFalse (lock.acquire (2, TimeUnit.SECONDS)) is returned; / / release lock lock.release () / / lock2 attempted to acquire the lock successfully because the lock has been released Assert.assertTrue (lock2.acquire (2, TimeUnit.SECONDS)); lock2.release ();} shared reentrant lock public void sharedReentrantLock () throws Exception {/ / create reentrant lock InterProcessLock lock = new InterProcessMutex (client, lockPath); / / lock2 is used to impersonate other clients InterProcessLock lock2 = new InterProcessMutex (client2, lockPath); / / lock acquires lock lock.acquire () Try {/ / lock acquired the lock lock.acquire () for the second time; try {/ / lock2 timed out to acquire the lock because the lock was already occupied by the lock client, so the acquisition failed. You need to wait for lock to release Assert.assertFalse (lock2.acquire (2, TimeUnit.SECONDS));} finally {lock.release () }} finally {/ / reentrant lock acquisition and release need to correspond one to one. If the lock is acquired twice and released once, then the lock is still occupied. If you comment on the following line of code, you will find that the following lock2 acquisition lock failed lock.release () } / / after lock is released, lock2 can acquire lock Assert.assertTrue (lock2.acquire (2, TimeUnit.SECONDS)); lock2.release ();} share reentrant read-write lock @ Testpublic void sharedReentrantReadWriteLock () throws Exception {/ / create read-write lock object, and Curator implements InterProce***eadWriteLock lock = new InterProce***eadWriteLock (client, lockPath) as fair lock. / / lock2 is used to impersonate other clients InterProce***eadWriteLock lock2 = new InterProce***eadWriteLock (client2, lockPath); / / use lock to simulate read operations / / use lock2 to simulate write operations / / acquire read locks (implemented using InterProcessMutex, so it is reentrant) InterProcessLock readLock = lock.readLock () / / acquire write lock (implemented in InterProcessMutex, so it can be reentered) InterProcessLock writeLock = lock2.writeLock (); / * * read-write lock test object * / class ReadWriteLockTest {/ / test data change field private Integer testData = 0; private Set threadSet = new HashSet () / / write data private void write () throws Exception {writeLock.acquire (); try {Thread.sleep (10); testData++; System.out.println ("write data\ t" + testData);} finally {writeLock.release () }} / / read data private void read () throws Exception {readLock.acquire (); try {Thread.sleep (10); System.out.println ("read data\ t" + testData);} finally {readLock.release () }} / / wait for the thread to finish, preventing the current thread from exiting directly after the test method call is completed, causing the console to fail to output information public void waitThread () throws InterruptedException (Thread thread: threadSet) {thread.join () }} / / create thread method 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 ();} / / Test method public void test () {for (int I = 0; I < 5; iTunes +) {createThread (1);} for (int I = 0; I < 5; iTunes +) {createThread (2) } ReadWriteLockTest readWriteLockTest = new ReadWriteLockTest (); readWriteLockTest.test (); readWriteLockTest.waitThread ();}

The test results are as follows:

Write data 1

Write data 2

Read data 2

Write data 3

Read data 3

Write data 4

Read data 4

Read data 4

Write data 5

Read data 5

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report