karelpeeters
10/05/2017, 5:39 AMtrue
or false
, not on the variable. For this reason it's a really bad idea to ever use a primitive for synchronization.janvladimirmostert
10/05/2017, 5:43 AMkarelpeeters
10/05/2017, 5:45 AMInt
for something completely different? You get nearly unreproducable deadlocks.data class ID(val id: Int)
and syncronize on that if you really need to.janvladimirmostert
10/05/2017, 5:54 AMsynchronize("project-344") { }
Once we need to scale to more than one instance, we'll obviously need to re-visit this and implement a better solutionkarelpeeters
10/05/2017, 7:08 AMString
won't work either, there's no guarantee two equal strings will be the same instance, see the common "why doesn't "test" == String("test")
work?".val lock = Object()
lock?janvladimirmostert
10/05/2017, 7:14 AMobject Lock {
val projectMap = ConcurrentHashMap<Long, ProjectVO>()
}
// synchronize on project id
inline fun <R> projectLock(projectId: Long, block: () -> R): R =
synchronized(Lock.projectMap.putIfAbsent(projectId, ProjectVO()) ?: ProjectVO()) {
block()
}
That workskarelpeeters
10/05/2017, 8:40 AMjanvladimirmostert
10/05/2017, 8:45 AMkarelpeeters
10/05/2017, 8:54 AMaddChild
of your DB on a single childLock
is nog enough?janvladimirmostert
10/05/2017, 8:56 AMkarelpeeters
10/05/2017, 8:57 AMjanvladimirmostert
10/05/2017, 8:59 AMkarelpeeters
10/05/2017, 9:01 AMjanvladimirmostert
10/05/2017, 9:02 AM