这个来自编程采访元素的问题声称,在以下代码中存在一个死锁:“ U1启动到U2的传输,然后U2立即启动到U1的传输。由于每次传输都是在单独的线程中进行的,因此,第一个线程可以锁定U1,然后第二个线程可以锁定U2”
class Account:
_global_id = 0
def __init__(self, balance):
self._balance = balance
self._id = Account._global_id
Account._global_id += 1
self._lock = threading.RLock()
def get_balance(self):
return self._balance
@staticmethod
def transfer(acc_from, acc_to, amount):
th = threading.Thread(target=acc_from._move, args=(acc_to, amount))
th.start()
def _move(self, acc_to, amount):
with self._lock:
if amount > self._balance:
return False
acc_to._balance += amount
self._balance -= amount
print('returning True')
return True
在这种情况下,我看不出有什么死锁。 U1和U2具有单独的锁,据我所知,线程_1只是锁定U1,而线程2则锁定U2,因为_move方法仅使用self._lock而不接触acc_to._lock。我想念什么?
答案 0 :(得分:0)
我也有同样的疑问,在on npm上没有发现已知的错误。根据此问题之前部分对死锁的解释,我认为应该是
CREATE VIEW ViewA AS
SELECT meter_reader, SUM(meter)
FROM reading
WHERE TRUNC(date_column, 'Month') = TRUNC(sysdate, 'Month')
GROUP BY meter_reader -- whatever that column is
代替
CREATE VIEW ViewA AS
SELECT meter_reader, SUM(meter)
FROM reading
WHERE date_column >= TRUNC(sysdate, 'Month') AND
date_column < TRUNC(sysdate, 'Month') + INTERVAL '1' MONTH
GROUP BY meter_reader -- whatever that column is
因为这是发生死锁的主要方式。