locking

In Java, how do I test if an object's monitor is locked? [duplicate]

独自空忆成欢 提交于 2019-12-02 04:58:17
This question already has an answer here: How do determine if an object is locked (synchronized) so not to block in Java? 7 answers Java: How to check if a lock can be acquired? [duplicate] 3 answers In Java, how do I test if an object's monitor is locked? In other words, given a object obj, does any thread own obj's monitor? I do not care which thread owns the monitor. All I need to test is if ANY thread owns a given object's monitor. Since a thread other than the current thread could own the monitor, Thread.holdsLock( obj ) is not enough as it only checks the current thread. I am trying to

Read-write lock with only one underlying lock?

自作多情 提交于 2019-12-02 04:55:46
I've written a read-write lock using Python's concurrency primitives (I think!). Every implementation I've read on SO or elsewhere seems to use 2 locks -- one for reads, and another for writes. My implementation contains only one monitor for reads, but I may be missing something crucial -- can anyone confirm that this will work? If so, what is the benefit to using an additional write lock? This is the classic read-write lock with preference for readers (may starve writers). I use a dummy cache to demonstrate the reading and writing. import threading as t class ReadWriteCache(object): def _

why does the following code result in deadlock

泄露秘密 提交于 2019-12-02 04:20:39
问题 I have the following class public class LockTester implements Runnable{ private static Locker locker = new Locker(); public static void main(String[] args){ for(int i=0;i<10;i++){ Thread t = new Thread(new LockTester()); t.start(); } } public void run(){ for(int i=0;i<1000;i++){ locker.unlockFirst();//note unlocking here locker.lockFirst(); locker.lockSecond(); locker.unlockSecond(); locker.unlockFirst(); } } } and Locker class public class Locker{ private Lock lock1 = new ReentrantLock();

Solution to Deadlock: Lock Ordering

时光毁灭记忆、已成空白 提交于 2019-12-02 04:16:58
问题 In the following code a deadlock is possible if two threads simultaneously invoke the transaction() function, transposing different accounts. void transaction(Account from, Account to, double amount) { mutex lock1, lock2; lock1 = getlock(from); lock2 = getlock(to); acquire(lock1); acquire(lock2); withdraw(from, amount); deposit(to, amount); release(lock2); release(lock1); } That is, one thread might invoke transaction(checkingaccount, savingsaccount, 25); and another might invoke transaction

Obtain Update Table Lock at start of Stored Procedure in SQL Server

纵饮孤独 提交于 2019-12-02 03:41:23
问题 I'm writing a SQL Server stored procedure in which I want to lock a table for update before executing the body of the stored procedure. I don't want to prevent other processes from reading the table, but I do want to prevent other processes updating the table. Here is my first attempt: CREATE PROCEDURE someProcedure BEGIN SET TRANSACTION ISOLATION LEVEL READ COMITTED BEGIN TRANSANCTION SELECT COUNT(*) FROM TheTable WITH (UPDLOCK, TABLOCK) -- Pause procedure so that we can view the locks with

php blocking when calling the same file concurrently

馋奶兔 提交于 2019-12-02 03:37:47
i'm having some really strange problem. i wrote a filemanager in PHP with the ability to download files -- which works fine. the whole script is built as one big file. now, while downloading a big file i'm not able to use the script at the same time for, say, browsing folder contents. it does nothing but keep loading. as soon as the download is finished everything works again. is there something that prevents PHP from parsing the same file concurrently? because other scripts work like a charm, no matter if i'm downloading or not. help or links to documentation are highly appreciated :) Do you

how to obtain a lock in two places but release on one place?

谁都会走 提交于 2019-12-02 03:07:56
问题 i'm newbie in c#. I need to obtain lock in 2 methods, but release in one method. Will that work? public void obtainLock() { Monitor.Enter(lockObj); } public void obtainReleaseLock() { lock (lockObj) { doStuff } } Especially can I call obtainLock and then obtainReleaseLock ? Is "doubleLock" allowed in C#? These two methods are always called from the same thread, however lockObj is used in another thread for synchronization. upd: after all comments what do you think about such code? is it ideal

Should this class use data locking for multi threading?

旧街凉风 提交于 2019-12-02 03:03:01
问题 I have a class that contains some data and there are many threads use it: class MyClass { static Dictionary<Key, Value> MyData; static IEnumerable<Data> Data { get { return MyData.Values; } } static void Reset() { MyData = GetMyData(); } } Sometime (say once in a day) the Reset method is called. I don't want to add locking because of performance, but not sure if everything will work without it. The question is: should I use any type of locking in this class? 回答1: I disagree with Richard. You

Right way to do a Parallel.For to compute data from Array

南笙酒味 提交于 2019-12-02 02:52:44
问题 want to: sum x and sum x*x. Where x = line[i]. Because more than one thread wants to read/write to the "sumAll" and "sumAllQ" I need to lock its access. The problem is that the lock kind off serializes things here. I would need to split this operation in #"Environment.ProcessorCount" for loops, each one summing one part of the array, and finally summing theirs results. But how can I make it programmatically? Sample code: //line is a float[] Parallel.For(0, line.Length, new ParallelOptions {

php blocking when calling the same file concurrently

一曲冷凌霜 提交于 2019-12-02 02:40:26
问题 i'm having some really strange problem. i wrote a filemanager in PHP with the ability to download files -- which works fine. the whole script is built as one big file. now, while downloading a big file i'm not able to use the script at the same time for, say, browsing folder contents. it does nothing but keep loading. as soon as the download is finished everything works again. is there something that prevents PHP from parsing the same file concurrently? because other scripts work like a charm