locking

Monitoring lock contention on Java applications

拥有回忆 提交于 2019-12-03 08:47:35
I am trying to create a little benchmark (in Groovy) that shows high thread contention on a couple of synchronized methods. High contention should show up when monitoring voluntary context switches, and in Linux this can be achieved thanks to "pidstat". The program is the following: class Res { private int n; synchronized public void inc() { n++; def foo = [] for (int i = 0; i < 1000; ++i) foo << "hello" } synchronized public int getN() { return n; } } while (true) { Res res = new Res() int N = 100000 for (int i = 0; i < N; ++i) { new Thread({ res.inc() if (res.getN() == N) { println "ok" } })

Checking whether the current thread owns a lock

好久不见. 提交于 2019-12-03 08:46:36
问题 Suppose I have the following code: public class SomeClass() { private readonly object _lock = new object(); public void SomeMethodA() { lock (_lock) { SomeHelperMethod(); //do something that requires lock on _lock } } public void SomeMethodB() { lock (_lock) { SomeHelperMethod(); //do something that requires lock on _lock } } private void SomeHelperMethod() { lock (_lock) { //do something that requires lock on _lock } } } Locking inside SomeHelperMethod seems redundant and wasteful, since all

Retry mechanism for optimistic locking (spring data + JPA)

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We decided on using optimistic locking in our web application in order to increase concurrency and without the using of pessimistic locking. We are now on a lookout for retry solutions. We would like to have as little impact as possible to our current code base. One of the solutions we saw on the web is using a retry interceptor with annotation to mark a method as retry able. Problem is we would like to annotate methods that are having the @Transactional annotation on them but the interceptor fails to retry them for some reason. (the

Locking a branch in perforce?

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Currently after I create a release branch, but when we have some time before we release, I sometimes open the entire branch for edit and then lock all files in order to prevent anybody from modifying anything during "code-freeze" period in the release branch. Is there a better way? Doing it my current way seems possibly like an incorrect use of the lock feature, is there a better way to keep somebody from checking in code without using branches. I though of P4 protect but I am not the admin on this perforce instance, and also dealing with

What resource does a key lock actually lock?

亡梦爱人 提交于 2019-12-03 08:35:29
问题 I know a key lock locks a key in an index. However, what does "key" actually mean? For example, if I have a non-clustered index on a surname column and attempt an update where surname = "Jones", will I have effectively locked every row in the table where the surname is "Jones"? Or will the index be locked at a higher level, preventing access of rows with surnames other than "Jones"? The reason I ask is this note in Books Online about Lock Granularity and Hierarchies: KEY: A row lock within an

Is the ConcurrentDictionary thread-safe to the point that I can use it for a static cache?

倖福魔咒の 提交于 2019-12-03 08:35:24
问题 Basically, if I want to do the following: public class SomeClass { private static ConcurrentDictionary<..., ...> Cache { get; set; } } Does this let me avoid using lock s all over the place? 回答1: Yes, it is thread safe and yes it avoids you using locks all over the place (whatever that means). Of course that will only provide you a thread safe access to the data stored in this dictionary, but if the data itself is not thread safe then you need to synchronize access to it of course. Imagine

rails - implementing a simple lock to prevent user's from editing the same data concurrently

混江龙づ霸主 提交于 2019-12-03 07:56:44
问题 I have an app where I need to prevent users from editing data while it is being edited by a different user. I'm trying to think of the best way to do it and wanted to ask for ideas. So far, I've created a settings model that stores application wide configuration on the db in key/value pairs. So, for the lock, I have a settings instance that's called LOCKED_TABLE_UID, and it stored the user_id of the user editing the table or null (nil) if the table is free. >> lock = Setting.find_by_key(

Why is an IX-lock compatible with another IX-lock in InnoDB?

孤人 提交于 2019-12-03 07:35:00
According to innodb lock mode lock type compatibility matrix X IX S IS X Conflict Conflict Conflict Conflict IX Conflict Compatible Conflict Compatible S Conflict Conflict Compatible Compatible IS Conflict Compatible Compatible Compatible IX is compatible with IX, but the fact is if we acquire one IX lock by select c1 from z where c1 = 1 for update in session 1, trying to acquire IX by select c1 from z where c1 = 1 for update will be blocked in session 2, so I think they are not compatible. Did I miss anything here? Edit after answer accepted: The reason why SELECT ... FOR UPDATE in one

Is it possible to have more than 32 locks in ConcurrentHashMap

点点圈 提交于 2019-12-03 07:15:18
I read ConcurrentHashMap works better in multi threading than Hashtable due to having locks at bucket level rather than map wide lock. It is at max 32 locks possible per map. Want to know why 32 and why not more than 32 locks. If you're talking about the Java ConcurrentHashMap , then the limit is arbitrary : Creates a new map with the same mappings as the given map. The map is created with a capacity of 1.5 times the number of mappings in the given map or 16 (whichever is greater), and a default load factor (0.75) and concurrencyLevel (16). If you read the source code it becomes clear that the

Sharing a Java synchronized block across a cluster, or using a global lock?

三世轮回 提交于 2019-12-03 07:09:51
问题 I have some code that I want to only allow access to by one thread. I know how to accomplish this using either synchronized blocks or methods, but will this work in a clustered environment? The target environment is WebSphere 6.0, with 2 nodes in the cluster. I have a feeling that synchronized won't work, since each instance of the application on each node will have its own JVM, right? What I am trying to do here is perform some updates to database records when the system is booted. It will