English 中文(简体)
协同效应, 何时使用或不使用?
原标题:Synchronization, When to or not to use?

I have started learning concurrency and threads in Java. I know the basics of synchronized (i.e. what it does). Conceptually I understand that it provides mutually exclusive access to a shared resource with multiple threads in Java. But when faced with an example like the one below I am confused about whether it is a good idea to have it synchronized. I know that critical sections of the code should be synchronized and this keyword should not be overused or it effects the performance.

public static synchronized List<AClass> sortA(AClass[] aArray) 
{
    List<AClass> aObj = getList(aArray);

    Collections.sort(aObj, new AComparator());

    return aObj;
}

public static synchronized List<AClass> getList(AClass[] anArray) 
{
    //It converts an array to a list and returns
}
最佳回答

假设每个校对通行证不同阵列,则不需要同步,因为其他变量是当地变量。

If instead you fire off a few threads all calling sortA and passing a reference to the same array, you d be in trouble without synchronized, because they would interfere with eachother.

请注意,从<代码>getList方法回归新的<代码>就可以看出。 清单,从一个阵列中选取,即使read穿相同阵列,你也获得了不同的<代码>List物体。 这是一种误导。 例如,使用<代码>Arrays.asList,由特定阵列支持,但javadoc明确指出: 对“标准通过”返回阵列清单的改动是谨慎的。

问题回答

当你在多个职业之间分享数据时,通常需要同步化,而且有可能修改数据,造成不一致。 如果数据为读数据,那么你就迫切需要同步。

在上述法典中,没有共享数据。 这些方法涉及所提供的投入和产出的回报。 如果多条镜子使用你的方法之一,每一条插手都有自己的投入和产出。 因此,在任何地方都不可能出现不一致的情况。 因此,您在以上几页文中的方法无需混淆。

如果使用不必要,协同效应一定会因所涉间接费用而降低业绩,因此只有在需要时才能谨慎使用。

你的静态方法并不取决于任何共同的国家,因此不必同步。

没有像在同步使用时那样的规则,如果你确信你的法典不会同时使用,那么你就可以避免使用同步化。

你正确表示的协同效应对你申请的用意产生影响,也可能导致饥饿。

基本上,所有收款都不应被阻止,因为根据一致的一揽子方案,收款已经执行。

如您所示,所有电话读物都将通过自己的阵容拷贝getList 。 无需同步使用<代码> ortA。 由于所有其他变量均为当地变量,因此采用的方法也是一样。

当地变量生活在 st锁之中,每read都有自己的 st子,因此其他read子不能干预。

如果你的要求没有改变目标状态,你不需要同步。

单版代码上的I.n t use synchronized。 i. 如果没有机会,可由多个线索进入物体。

This may appear obvious but ~99% of StringBuffer used in the JDK can only be used by one thread can be replaced with a StringBuilder (which is not synchronized)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签