jBuilderUtils is a small library with two types:
ReadWriteLock and AtomicInteger.
AtomicInteger is a simple counter that can be accessed from multiple timers.
ReadWriteLock:
https://en.wikipedia.org/wiki/Readers–writer_lock
In the case of CloudKVS, only the write lock is used. Only one thread can enter the code protected with this lock.
The AddItem sub, first calculates the current maximum id and then inserts the item with id + 1. The operation must be atomic. The database cannot be modified in the middle.
I don't recommend using the ReadWriteLock unless you understand the server threading model. In most cases it is not required. Using locks incorrectly can easily hang to server.
Two simple solutions:
1. Use a single threaded servlet.
2. Use CallSubDelayed to call a code module. The code will run on the main thread.