You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
staticclassCakeEaterimplementsRunnable {
privatefinalAmazonDynamoDBLockClientlockClient;
privatefinalStringuserName;
privatefinallongeatingSpeed;
publicCakeEater(AmazonDynamoDBLockClientlockClient, StringuserName, longeatingSpeed) {
this.lockClient = lockClient;
this.userName = userName;
this.eatingSpeed = eatingSpeed;
}
@Overridepublicvoidrun(){
if (cakePieces > 0) {
finalOptional<LockItem> lockItem;
try {
System.out.println(userName + ": trying to get lock");
lockItem =
lockClient.tryAcquireLock(AcquireLockOptions.builder(PARTITION_KEY).withShouldSkipBlockingWait(true).build());
} catch (InterruptedExceptione) {
thrownewRuntimeException(e);
}
if (lockItem.get() != null ) {
System.out.println(userName + " got key: " + lockItem);
try {
Thread.sleep(eatingSpeed); // Simulate eating time
} catch (InterruptedExceptione) {
thrownewRuntimeException(e);
}
cakePieces -= 1;
System.out.println(userName + " ate a piece of cake. Remaining pieces: " + cakePieces);
lockClient.releaseLock(lockItem.get());
} else
{
System.out.println(userName + " is waiting a lock");
try {
thrownewException("lock is held by other");
} catch (Exceptione) {
thrownewRuntimeException(e);
}
}
}
}
}
}
Sep 09, 2024 12:26:26 AM io.harry.utils.DynamoDbClientTest$CakeEater run
INFO: User 2: trying to get lock
Sep 09, 2024 12:26:26 AM io.harry.utils.DynamoDbClientTest$CakeEater run
INFO: User 1: trying to get lock
Sep 09, 2024 12:26:27 AM io.harry.utils.DynamoDbClientTest$CakeEater run
INFO: User 1 got key: Optional[LockItem{Partition Key=key, Sort Key=Optional.empty, Owner Name=HG976G70QVf8210aae-73ff-45c8-9a19-4c541e5925ff, Lookup Time=917462271, Lease Duration=50000, Record Version Number=0269d63d-8844-40e5-8cdc-80289cf92326, Delete On Close=true, Data=, Is Released=false}]
Sep 09, 2024 12:26:47 AM io.harry.utils.DynamoDbClientTest$CakeEater run
INFO: User 1 ate a piece of cake. Remaining pieces: 9
I was expecting use2 raise exception after user 1 got the lock
The text was updated successfully, but these errors were encountered:
Kiollpt
changed the title
Why multi-threading is blocking with configuing withShouldSkipBlockingWait = True
Does the lock support multi-threading ?
Sep 8, 2024
Kiollpt
changed the title
Does the lock support multi-threading ?
Why multi-threading is blocking with configuing withShouldSkipBlockingWait = True
Sep 8, 2024
main
CakeEater
I was expecting use2 raise exception after user 1 got the lock
The text was updated successfully, but these errors were encountered: