java concurrency: many writers, one reader

前端 未结 9 2088
我寻月下人不归
我寻月下人不归 2021-01-30 18:22

I need to gather some statistics in my software and i am trying to make it fast and correct, which is not easy (for me!)

first my code so far with two classes, a StatsS

相关标签:
9条回答
  • 2021-01-30 18:56

    Another alternative for implement both methods using ReentranReadWriteLock. This implementation protects against race conditions at getStats method, if you need to clear the counters. Also it removes the mutable AtomicLong from the getStats an uses an immutable Long.

    public class StatsService {
    
        private final Map<String, AtomicLong> stats = new HashMap<String, AtomicLong>(1000);
        private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
        private final Lock r = rwl.readLock();
        private final Lock w = rwl.writeLock();
    
        public void  notify(final String key) {
            r.lock();
            AtomicLong count = stats.get(key);
            if (count == null) {
                r.unlock();
                w.lock();
                count = stats.get(key);
                if(count == null) { 
                    count = new AtomicLong();
                    stats.put(key, count);
                }
                r.lock();
                w.unlock();
            }
            count.incrementAndGet();
            r.unlock();
        }
    
        public Map<String, Long> getStats() {
            w.lock();
    
            Map<String, Long> copy = new HashMap<String, Long>();
            for(Entry<String,AtomicLong> entry : stats.entrySet() ){
                    copy.put(entry.getKey(), entry.getValue().longValue());
            }
            stats.clear();
            w.unlock();
    
            return copy;
        }
    }
    

    I hope this helps, any comments are welcome!

    0 讨论(0)
  • 2021-01-30 18:59

    As jack was eluding to you can use the java.util.concurrent library which includes a ConcurrentHashMap and AtomicLong. You can put the AtomicLong in if absent else, you can increment the value. Since AtomicLong is thread safe you will be able to increment the variable without worry about a concurrency issue.

    public void notify(String key) {
        AtomicLong value = stats.get(key);
        if (value == null) {
            value = stats.putIfAbsent(key, new AtomicLong(1));
        }
        if (value != null) {
            value.incrementAndGet();
        }
    }
    

    This should be both fast and thread safe

    Edit: Refactored sligthly so there is only at most two lookups.

    0 讨论(0)
  • 2021-01-30 19:06

    Have you looked into ScheduledThreadPoolExecutor? You could use that to schedule your writers, which could all write to a concurrent collection, such as the ConcurrentLinkedQueue mentioned by @Chris Dail. You can have a separately schedule job to read from the Queue as necessary, and the Java SDK should handle pretty much all your concurrency concerns, no manual locking needed.

    0 讨论(0)
  • 2021-01-30 19:07

    Chris Dail's answer looks like a good approach.

    Another alternative would be to use a concurrent Multiset. There is one in the Google Collections library. You could use this as follows:

    private Multiset<String> stats = ConcurrentHashMultiset.create();
    
    public void notify ( String key )
    {
        stats.add(key, 1);
    }
    

    Looking at the source, this is implemented using a ConcurrentHashMap and using putIfAbsent and the three-argument version of replace to detect concurrent modifications and retry.

    0 讨论(0)
  • 2021-01-30 19:08

    Here is how to do it with minimal impact on the performance of the threads being measured. This is the fastest solution possible in Java, without resorting to special hardware registers for performance counting.

    Have each thread output its stats independently of the others, that is with no synchronization, to some stats object. Make the field containing the count volatile, so it is memory fenced:

    class Stats
    {
       public volatile long count;
    }
    
    class SomeRunnable implements Runnable
    {
       public void run()
       {
         doStuff();
         stats.count++;
       }
    }
    

    Have another thread, that holds a reference to all the Stats objects, periodically go around them all and add up the counts across all threads:

    public long accumulateStats()
    {
       long count = previousCount;
    
       for (Stats stat : allStats)
       {
           count += stat.count;
       }
    
       long resultDelta = count - previousCount;
       previousCount = count;
    
       return resultDelta;
    }
    

    This gatherer thread also needs a sleep() (or some other throttle) added to it. It can periodically output counts/sec to the console for example, to give you a "live" view of how your application is performing.

    This avoids the synchronization overhead about as much as you can.

    The other trick to consider is padding the Stats objects to 128 (or 256 bytes on SandyBridge or later), so as to keep the different threads counts on different cache lines, or there will be caching contention on the CPU.

    When only one thread reads and one writes, you do not need locks or atomics, a volatile is sufficient. There will still be some thread contention, when the stats reader thread interacts with the CPU cache line of the thread being measured. This cannot be avoided, but it is the way to do it with minimal impact on the running thread; read the stats maybe once a second or less.

    0 讨论(0)
  • 2021-01-30 19:10

    I would suggest taking a look at Java's util.concurrent library. I think you can implement this solution a lot cleaner. I don't think you need a map here at all. I would recommend implementing this using the ConcurrentLinkedQueue. Each 'producer' can freely write to this queue without worrying about others. It can put an object on the queue with the data for its statistics.

    The harvester can consume the queue continually pulling data off and processsing it. It can then store it however it needs.

    0 讨论(0)
提交回复
热议问题