Java的synchronized、semaphore和queue的性能

huangapple go评论73阅读模式
英文:

Performance of Java synchronized, semaphore, and queue

问题

  1. synchronized 的性能是否足够处理这个情况?如果我的计算机有16个核心而不是4个,那么随着核心数增加,处理变得更昂贵吗?

     // 这个方法会被多个线程调用
     public synchronized void record(String alias, long result, int delta) {
       // 在这里计算自定义统计信息...(简短且快速)
     }
    
  2. 另一个考虑的选项是使用 Semaphore 来访问关键部分。它们是否比选项#1更快?它们在幕后仍然使用锁吗?

     private Semaphore s = new Semaphore(1);
    
     public void record(String alias, long result, int delta) 
         throws InterruptedException {
       boolean acquired = false;
       try {
         this.s.acquire();
         acquired = true;
         // 在这里计算自定义统计信息...(简短且快速)
       } finally {
         if (acquired) this.s.release();
       }
     }
    
  3. 最后,我考虑了使用队列(每个调用者作为生产者 + 一个 Executor 作为消费者),但我希望避免额外的线程/执行器作为消费者。

  4. 是否有其他更好的选择?

最后但并非最不重要的,对于我每秒5000次的使用情况,我是不是过于担心了?如果是这样,我将使用最简单的选项(对方法进行同步)来控制并发。

英文:

I'm writing a small piece of code that will be called by many threads simultaneously to gather statistics on processes. The code is short and quick but must be somehow synchronized to keep stats correct. The typical load would be around 100-200 calls per second but can escalate to 5000 calls per second at some point during the day.

  1. Is synchronized to slow to handle this? If I have 16 cores as opposed to 4, does it get more expensive to handle the more cores I have?

     // This method is called by multiple threads
     public synchronized void record(String alias, long result, int delta) {
       // compute custom stats here... (short & quick)
     }
    
  2. The other option I was considering is to use a Semaphore to access the critical section. Are they faster than option #1? Do they still use locks behind the scenes?

     private Semaphore s = new Semaphore(1);
    
     public void record(String alias, long result, int delta) 
         throws InterruptedException {
       boolean acquired = false;
       try {
         this.s.acquire();
         acquired = true;
         // compute custom stats here... (short & quick)
       } finally {
         if (acquired) this.s.release();
       }
     }
    
  3. Finally, I was considering a Queue (each caller is a producer + one Executor as consumer) but I wanted to avoid an extra thread/executor as the consumer.

  4. Other better option?

Last but not least, for my use case of 5000 calls per second am I worrying too much? If so I would use the simplest option (synchronize the method) to control concurrency.

答案1

得分: 1

如果您的计算量较小且速度较快,则使用信号量与使用同步锁(synchronized)并没有太大优势。对于消息的并发处理,生产者/消费者模式是可行的方法。您可以使用阻塞队列,然后启动多个消费者线程。

英文:

If your compute is short and quick, then using semaphore vs synchronized is not really going to give you a lead. For concurrent processing of messages, producer/consumer is the way to go. You can use blocking queue and then spin multiple consumer threads.

huangapple
  • 本文由 发表于 2020年9月11日 09:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63839633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定