Java同步和性能方面

Java synchronization and performance in an aspect(Java同步和性能方面)

本文介绍了Java同步和性能方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到我需要在一个方面同步大量数据收集代码,但性能是一个真正的问题.如果性能下降太多,我的工具将被淘汰.我将分别编写整数和长整数以及各种数组、ArrayLists 和 Maps.应用程序的多个线程将进行函数调用,这些调用将被我的方面拾取.我应该注意哪些会对性能产生负面影响的事情?哪些代码模式更有效?

I just realized that I need to synchronize a significant amount of data collection code in an aspect but performance is a real concern. If performance degrades too much my tool will be thrown out. I will be writing ints and longs individually and to various arrays, ArrayLists and Maps. There will be multiple threads of an application that will make function calls that will be picked up by my aspect. What kind of things should I look out for that will negatively affect performance? What code patterns are more efficient?

特别是我有一个调用许多其他数据记录方法的方法:

In particular I have a method that calls many other data recording methods:

void foo() {
    bar();
    woz();
    ...
}

这些方法主要是增加方面字段的增量

The methods mostly do adding an incrementing of aspect fields

void bar() {
    f++; // f is a field of the aspect
    for (int i = 0; i < ary.length; i++) {
        // get some values from aspect point cut
        if (some condiction) {
            ary[i] += someValue; // ary a field of the aspect
        }
     }
 }

我应该单独同步 foo 或 bar、woz 等,还是应该将 bar、woz 等中的所有代码移动到 foo 中并同步它?我应该在 this 上同步一个专门创建的同步对象吗:

Should I synchronize foo, or bar, woz and others individually, or should I move all the code in bar, woz, etc into foo and just synchronize it? Should I synchronize on this, on a specifically created synchronization object:

private final Object syncObject = new Object();

(请参阅 this 帖子),或关于方法中的单个数据元素:

(see this post), or on individual data elements within the methods:

ArrayList<Integer> a = new ArrayList<Integer>();

void bar() {    
    synchronize(a) {
        // synchronized code
    }
}

推荐答案

并发非常棘手.做错很容易,做对也很难.在这一点上,我不会太担心性能.我首先关心的是让并发代码安全地工作(没有死锁或竞争条件).

Concurrency is extremely tricky. It's very easy to get it wrong, and very hard to get right. I wouldn't be too terribly worried about performance at this point. My first and foremost concern would be to get the concurrent code to work safely (no deadlocks or race conditions).

但在性能问题上:如有疑问,请说明.很难说不同的同步方案将如何影响性能.我们更难给你建议.我们需要查看更多您的代码,并更深入地了解应用程序的功能,以便为您提供真正有用的答案.相比之下,分析为您提供了确凿的证据,证明一种方法是否比另一种方法慢.它甚至可以帮助您确定减速的位置.

But on the issue of performance: when in doubt, profile. It's hard to say just how different synchronization schemes will affect performance. It's even harder for us to give you suggestions. We'd need to see a lot more of your code and gain a much deeper understanding of what the application does to give you a truly useful answer. In contrast, profiling gives you hard evidence as to if one approach is slower than another. It can even help you identify where the slowdown is.

现在有很多很棒的 Java 分析工具.Netbeans 和 Eclipse 分析器很好.

There are a lot of great profiling tools for Java these days. The Netbeans and Eclipse profilers are good.

另外,我建议完全远离原始同步.尝试使用 java.util.concurrency 包中的一些类.它们使编写并发代码变得更加容易,并且更不容易出错.

Also, I'd recommend staying away from raw synchronization altogether. Try using some of the classes in the java.util.concurrency package. They make writing concurrent code much easier, and much less error prone.

另外,我建议您阅读 Brian Goetz 等人撰写的 Java 并发实践.它写得很好,涵盖了很多领域.

Also, I recommend you read Java Concurrency in Practice by Brian Goetz, et al. It's very well written and covers a lot of ground.

这篇关于Java同步和性能方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java同步和性能方面

基础教程推荐