awslabs / amazon-dynamodb-lock-client

The AmazonDynamoDBLockClient is a general purpose distributed locking library built on top of DynamoDB. It supports both coarse-grained and fine-grained locking.
Other
472 stars 85 forks source link

Avoid `synchronized` during blocking operations (loom friendliness) #97

Open ravi-signal opened 7 months ago

ravi-signal commented 7 months ago

Virtual threads, introduced in JEP 444, are forced to pin to their carrier threads when in a synchronized block. Since AmazonDynamoDBLockClient synchronizes on LockItems while performing blocking dynamodb operations, using the lock client from a virtual thread pins the virtual thread to the carrier thread while waiting for the operation to complete (which sort of defeats the purpose of virtual threads).

JEP 444 suggests replacing synchronzied blocks that contain long blocking I/O operations with ReentrantLock

avoid frequent and long-lived pinning by revising synchronized blocks or methods that run frequently and guard potentially long I/O operations to use java.util.concurrent.locks.ReentrantLock instead.

I'd be happy to open a PR if you're amenable to tossing a ReentrantLock in LockItem to replace the synchronized blocks