HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Concurrent byte array access in Java with different locking

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arraybytewithjavadifferentlockingconcurrentaccess

Problem

I have one writer thread and 10 reader threads of a byte array. It is important to synchronize writes and reads over a "row" (16 bytes).

There are a lot less locks than rows, e.g. in the current configuration 1024 (chunkRows) rows have one lock which makes the overall operation faster, but also consumes less memory.

Here is the read method for ReadWrite locking:

public void read(int pointer, byte[] tmpBytes, int length) {
    Lock lock = locks[pointer / rowSize / chunkRows].readLock();
    lock.lock();
    try {
        System.arraycopy(bytes, pointer, tmpBytes, 0, length);
    } finally {
        lock.unlock();
    }
}


and here for simple "Object"-synchronized:

public void read(int pointer, byte[] tmpBytes, int length) {
    Object lock = locks[pointer / rowSize / chunkRows];
    synchronized (lock) {
        System.arraycopy(bytes, pointer, tmpBytes, 0, length);
    }
}


Here is the full repository which you can easily run via maven. It uses JMH to avoid nasty performance pitfalls. Of course, also write is synchronized similarly.

Now why is my ReadWrite implementation a lot slower than synchronized? (RW is roughly 40% slower)

I like Object-locking more because it uses less RAM but did I made a mistake with ReadWrite-locking or does this make sense?

I tried it on my dual core laptop as well as on more beefy quad core commodity server with simmilar results (Object-locking faster than RW). I get something like 13.0 ops/s for ReadWrite locking and 21.6 ops/s for Object locking.

Update

Several things were wrong in the benchmark which I fixed (maybe there are still glitches)

  • Very important bug fix was to include service.awaitTermination



  • make sure write and read happen at the same time and both are long enough (previously write was too short and nearly no r+w concurrency happened)



  • I've learned about StampedLock, could be slightly faster and also optimistic locking could be even the winner here.



  • minor thing: catch errors in runn

Solution

The documentation of the ReadWriteLock says:


Whether or not a read-write lock will improve performance over the use of a mutual exclusion lock depends on the frequency that the data is read compared to being modified, the duration of the read and write operations, and the contention for the data - that is, the number of threads that will try to read or write the data at the same time. For example, a collection that is initially populated with data and thereafter infrequently modified, while being frequently searched (such as a directory of some kind) is an ideal candidate for the use of a read-write lock. However, if updates become frequent then the data spends most of its time being exclusively locked and there is little, if any increase in concurrency. Further, if the read operations are too short the overhead of the read-write lock implementation (which is inherently more complex than a mutual exclusion lock) can dominate the execution cost, particularly as many read-write lock implementations still serialize all threads through a small section of code. Ultimately, only profiling and measurement will establish whether the use of a read-write lock is suitable for your application.

In your code, the read operations are very short, they are only copying 16 bytes. Probably, the overhead of using a more complex lock is dominating the cost of execution as stated in the documentation.

Context

StackExchange Code Review Q#140749, answer score: 3

Revisions (0)

No revisions yet.