locks

Does ConcurrentHashMap need synchronization when incrementing its values?

北城以北 提交于 2019-12-12 03:54:39
问题 I know ConcurrentHashMap is thread-safe e.g.putIfAbsent,Replace etc., but I was wondering, is a block of code like the one below safe? if (accumulator.containsKey(key)) { //accumulator is a ConcurrentHashMap accumulator.put(key, accumulator.get(key)+1); } else { accumulator.put(key, 0); } Keep in mind that the accumulator value for a key may be asked by two different threads simultaneously, which would cause a problem in a normal HashMap. So do I need something like this? ConcurrentHashMap

MySQL Row level locks

我的未来我决定 提交于 2019-12-11 02:44:34
问题 I am not sure about how the row level lock work but here is my problem I have a table T (id int , balance int ) ( engine = InnoDB ) that I want to lock rows having ID = 1, so i start a transaction like this : start transaction ; select * from T where ID = 1 FOR UPDATE ; Before sending commit, I wanted to try if really the rows are LOCKED. So I started an other session and I typed : UPDATE T set balance = balance + 100 where ID = 1 ; Here I clearly see that I waiting for the lock (timeout

How to avoid Nested synchronization and the resulting deadlock

旧时模样 提交于 2019-12-09 21:21:03
问题 I need to lock two objects in a functionality and the current code looke like this; Object obj1 = ...//get from somewhere Object obj2 = ...//get from somewhere synchronized(obj1){ ...//blah synchronized(obj2){ ...//blah } } As you can see this is a plain and straight recipe for deadlocks if another thread runs this piece of code with obj1 and two reversed. Is there a way to avoid this situation using concurrency-utils locks? I was contemplating maintaining a map of objects and their locks and

MySQL InnoDB locks on joined rows

蓝咒 提交于 2019-12-09 14:47:33
问题 Does "SELECT ... FOR UPDATE" lock joined rows in MySQL? If so, is it possible to disable this behaviour? There is nothing about this in the documentation. I've seen that Oracle supports "SELECT ... FOR UPDATE OF table_name" where table_name is the main table or one of the joined tables for which the affected rows will be locked, but I've never seen this mentioned in context with MySQL. 回答1: See this MySQL doc page. It says: A locking read, an UPDATE, or a DELETE generally set record locks on

How to check is any thread waiting on condition variable?

心已入冬 提交于 2019-12-08 04:05:27
问题 I have Condition variable named cond. Is there any method which could give me true or false if there is any thread awaiting on cond? I need something like: Boolean cond.isAwaitingSetEmpty() Thanks for help 回答1: If by "condition" you mean a Condition created by ReentrantLock.newCondition() , then you can use ReentrantLock.hasWaiters(Condition cond). 回答2: It's available from the Lock that the Condition is bound to: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks

How is class level locking achieved in java?

断了今生、忘了曾经 提交于 2019-12-06 23:38:59
I am aware of locking concepts with synchronization of static and non static methods to lock classes and instances respectively. What I am not able to understand is, how is class level lock achieved? I mean, class is a template only, with no physical significance. So, when we say that class level locking is achieved with synchronizing static methods what happens then? Do all the objects of that class get locked or some other process? With what I could find out with my search is that there are class objects (Class.class) and lock is acquired on this class object. But then how are all instances

How to check is any thread waiting on condition variable?

谁都会走 提交于 2019-12-06 16:58:46
I have Condition variable named cond. Is there any method which could give me true or false if there is any thread awaiting on cond? I need something like: Boolean cond.isAwaitingSetEmpty() Thanks for help If by "condition" you mean a Condition created by ReentrantLock.newCondition() , then you can use ReentrantLock.hasWaiters(Condition cond) . It's available from the Lock that the Condition is bound to: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html For example: getWaitingThreads(Condition condition) getWaitQueueLength(Condition condition) etc. 来源:

Custom Locks Threading python

爱⌒轻易说出口 提交于 2019-12-06 12:07:37
问题 Hi I am using PyQt4 and i need to implement locks in a QThread but this class does not have a method lock implemented like the library threading. Any idea how can I implement a lock here? I have a question if i use threading i implement the lock like this class Example: lock = threading.Lock() def __init__(self) pass def run(self): Example.lock.acquire() ....... ........ Example.lock.realease() is this the same?: class Example(QtCore.QThread): mutex = QtCore.QMutex()) def __init__(self) pass

How to avoid pickling errors when sharing objects between threads?

此生再无相见时 提交于 2019-12-04 16:43:01
I have a program in which I need to store a global variable into a file. I am doing this using the pickle module. I have another thread (Daemon= False , from threading module) which sometimes changes the value of the global variable. The value is also modified in global scope(the main program). I am dumping the value of the variable into a .pkl file every 5 seconds (using another thread from threading module). But I found the following error when dump method was executed: TypeError: can't pickle _thread.lock objects Why is this happening? And what can I do to fix it? Note: I have found some

Custom Locks Threading python

一世执手 提交于 2019-12-04 15:45:37
Hi I am using PyQt4 and i need to implement locks in a QThread but this class does not have a method lock implemented like the library threading. Any idea how can I implement a lock here? I have a question if i use threading i implement the lock like this class Example: lock = threading.Lock() def __init__(self) pass def run(self): Example.lock.acquire() ....... ........ Example.lock.realease() is this the same?: class Example(QtCore.QThread): mutex = QtCore.QMutex()) def __init__(self) pass def run(self): mutex.lock() ....... ........ mutex.unlock() Thanks You want the QMutex class. Qt uses