English 中文(简体)
Java阵容同步
原标题:Java array synchronization

我有一类任务管理阶层,其种类如下:

public static int[][][] tasks;

我基本上可以进入这样的牢房:

synchronized(tasks[A][B]) {
  // Doing something with tasks[A][B][0]
}

我的问题是,如果我这样做的话:

synchronized(tasks[A]) {
  // ...
}

will it also block threads trying to enter synchronized(tasks[A][B])? In other words, does a synchronized access to an array also synchronizes the accsess to it s cells? If not, how to I block the WHOLE array of tasks[A] for my thread?


Edit: the answer is NO. When someone is in a synchronized block on tasks[A] someone else can simultaniously be in a synchronized block on tasks[A][B] for example - because it s a different object. So when talking about accessing objects from one place at a time, arrays are no different: to touch object X from one place at a time you need to surround it by synchronized(X) EVERYWHERE you touch it.

最佳回答

内部[][][]是一系列的惯性阵列,因此,您的同步点正与最低层物体同步,形成一系列的愤怒,阻碍其他同步进入同一阵列。

而另一方面,同步(A)则将目标放在下一个层次上,即一系列集束阵列。 这妨碍了同步进入这一阵列,这意味着,实际上,任何使用同步的守则都将被阻断——这似乎是你想要的,只要你们的所有同级任务同步。

请注意,同步并不锁定任何东西! 然而,如果两条镜子试图在同一物体上同步,就不得不等待。

然后,你就另一个目标(大量愤怒)开展工作。

我害怕我说,谁回答是误导。 你做的是正确的事。

问题回答

每一阵列本身都是一个目标(有监控);阵列<>tasks[A]与阵列不同。 如果你想要同步使用<代码>tasks[A]的所有“子”阵列,解决办法就是说,你必须做<代码>同步化(tasks[A]。 如果all accesses into the hexachloroget (say, tasks[A][B]),那么任何进一步的同步都是多余的。

看来,你的根本问题是,实际上,“我怎么能安全地修改数据结构的>结构<>和contents,同时保持尽可能的一致?” 如果你把问题更多地放在问题空间上,我们就有可能更深入地探讨。 三维阵列可能不是最佳解决办法。

电话 我看到一些法典,即不同的mut或监测器,我的第一个反应是担心僵局;在你目前的法典中,你是否在同一个read子里设置了多个监测器? 如果是的话,你是否确保他们被锁定在每次的警示中?

如果你能够解释你想要达到什么目标,以及你如何使用/修改这个<代码>tasks阵列,那将大有助益。 <代码>java.util.con Current 足以使用单个监测器的电离不开(或也许没有把握)。 当然,这一切都取决于你努力做什么。 而且,如果你试图 gr笑这么多不同的锁,是因为你非常经常地阅读这些锁,那么,用一只读书锁处理整个3个有标记的弹 object可能足以满足你们的需要。





相关问题
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 ...

热门标签