Skip to content

Why multi-threading is blocking with configuing withShouldSkipBlockingWait = True #103

@Kiollpt

Description

@Kiollpt

main

  ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(new CakeEater(lockClient, "User 1", 20000));
        executorService.submit(new CakeEater(lockClient, "User 2",10000));

CakeEater

static class CakeEater implements Runnable {
        private final AmazonDynamoDBLockClient lockClient;
        private final String userName;
        private final long eatingSpeed;



        public CakeEater(AmazonDynamoDBLockClient lockClient, String userName, long eatingSpeed) {
            this.lockClient = lockClient;
            this.userName = userName;
            this.eatingSpeed = eatingSpeed;
        }

        @Override
        public void run(){

            if (cakePieces > 0) {

                final Optional<LockItem> lockItem;
              try {
                  System.out.println(userName + ": trying to get lock");

                  lockItem =
                          lockClient.tryAcquireLock(AcquireLockOptions.builder(PARTITION_KEY).withShouldSkipBlockingWait(true).build());
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }


              if (lockItem.get() != null ) {
                  System.out.println(userName + " got key: " + lockItem);

                  try {
                      Thread.sleep(eatingSpeed); // Simulate eating time
                  } catch (InterruptedException e) {
                      throw new RuntimeException(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 {
                      throw new Exception("lock is held by other");
                  } catch (Exception e) {
                      throw new RuntimeException(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions