Java方面的同步和性能 [英] Java synchronization and performance in an aspect

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

问题描述

我刚刚意识到我需要在一个方面同步大量的数据收集代码,但性能是一个真正的问题。如果性能下降太多,我的工具将被抛弃。我将单独编写int和long,以及各种数组,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();

(参见帖子),或方法中的各个数据元素:

(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.

另外,我建议你阅读 Java Concurrency in Practice 。它编写得非常好,涵盖了很多方面。

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方面的同步和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆