private static void personEat(int personId) { string person = $"Person({personId})"; // Try to acquire a lock maximum 5 times. ILockObject lockObject = _waitAndRetryPolicy .Execute( ctx => _lockFactory.AcquireLock(_lockKey, TimeSpan.FromSeconds(5)), new Dictionary <string, object> { { "person", person } }); if (lockObject.IsAcquired) { // We got a lock. Console.WriteLine($"{person} begin eat food."); Thread.Sleep(1000); // Try to release the lock. if (_random.NextDouble() < 0.8) { lockObject.Release(); Console.WriteLine($"{person} released the lock."); } else { Console.WriteLine($"{person} did not release lock."); } } else { Console.WriteLine($"{person} did not get food."); } }