semaphore

Android: wait for firebase valueEventListener

浪子不回头ぞ 提交于 2019-12-21 17:24:06
问题 I am trying to use Semaphore to wait for my firebase valueEventListener. I have an user info activity with 6 different fields that the user must fill out. When the user saves his/her info, I want to do an "all or nothing" type of check. Certain user info cannot be duplicated...for example user name, email, and phonenumber. I am using firebase and currently the general idea is of the format: void saveUserInfo(){ if(field1 exist in database){ return; } . . . if(field6 exist in database){ return

Java: What, if anything, is locked by synchronized methods apart from the object they belong to?

人盡茶涼 提交于 2019-12-21 15:02:08
问题 Now, I'm not sure whether this is a stupid question, please bear with me if it is. Is the lock on an object "recursive", i. e. if two objects have references to a third object in their fields and a thread is running a synchronized method on one of the two, can any other thread access the third object? // a and b are some objects that implement Runnable // they both reference the same third object a.ref = c; b.ref = c; // a is run in a thread and processes some data in a loop for a long time /

Java - Semaphore release without acquire

[亡魂溺海] 提交于 2019-12-21 10:48:53
问题 I have threads which are given random number (1 to n) and are instructed to print them in sorted order. I used semaphore such that I acquire the number of permits = random number and release one permit more than what was acquired. acquired = random number; released = 1+random number Initial permit count for semaphore is 1. So thread with random number 1 should get permit and then 2 and so on. This is supported as per the documentation given below There is no requirement that a thread that

Java - Semaphore release without acquire

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 10:48:23
问题 I have threads which are given random number (1 to n) and are instructed to print them in sorted order. I used semaphore such that I acquire the number of permits = random number and release one permit more than what was acquired. acquired = random number; released = 1+random number Initial permit count for semaphore is 1. So thread with random number 1 should get permit and then 2 and so on. This is supported as per the documentation given below There is no requirement that a thread that

Django: Simple rate limiting

﹥>﹥吖頭↗ 提交于 2019-12-21 05:24:12
问题 Many of my views fetch external resources. I want to make sure that under heavy load I don't blow up the remote sites (and/or get banned). I only have 1 crawler so having a central lock will work fine. So the details: I want to allow at most 3 queries to a host per second, and have the rest block for a maximum of 15 seconds. How could I do this (easily)? Some thoughts : Use django cache Seems to only have 1 second resolution Use a file based semaphore Easy to do locks for concurrency. Not

Locking with nested async calls

被刻印的时光 ゝ 提交于 2019-12-21 04:02:43
问题 I am working on a multi threaded WindowsPhone8 app that has critical sections within async methods. Does anyone know of a way to properly use semaphores / mutexes in C# where you are using nested async calls where the inner method may be acquiring the same lock that it already acquired up the callstack? I thought the SemaphoreSlim might be the answer, but it looks like it causes a deadlock. public class Foo { SemaphoreSlim _lock = new SemaphoreSlim(1); public async Task Bar() { await _lock

How and why can a Semaphore give out more permits than it was initialized with?

穿精又带淫゛_ 提交于 2019-12-21 03:56:06
问题 I am reading the book Java Concurrency in Practice. In a section about java.util.concurrent.Semaphore , the below lines are present in the book. It is a comment about its implementation of "virtual permit" objects The implementation has no actual permit objects, and Semaphore does not associate dispensed permits with threads, so a permit acquired in one thread can be released from another thread. You can think of acquire as consuming a permit and release as creating one; a Semaphore is not

Semaphore and synchronization

情到浓时终转凉″ 提交于 2019-12-21 03:45:36
问题 I could not quite understand the following from the semaphore description in javadocs. Note that no synchronization lock is held when acquire() is called as that would prevent an item from being returned to the pool. The semaphore encapsulates the synchronization needed to restrict access to the pool, separately from any synchronization needed to maintain the consistency of the pool itself. Can someone please help me understand this and its implications. 回答1: A semaphore acts as a limiter of

TPL Dataflow vs plain Semaphore

巧了我就是萌 提交于 2019-12-21 02:52:10
问题 I have a requirement to make a scalable process. The process has mainly I/O operations with some minor CPU operations (mainly deserializing strings). The process query the database for a list of urls, then fetches data from these urls, deserilize the downloaded data to objects, then persist some of the data into crm dynamics and also to another database. Afterwards I need to update the first database which urls were processed. Part of the requirement is to make the parallelism degree

What is the difference between .Semaphore() and .BoundedSemaphore()?

喜你入骨 提交于 2019-12-21 02:28:30
问题 I know that threading.Lock() is equal to threading.Semaphore(1) . Is also threading.Lock() equal to threading.BoundedSemaphore(1) ? And newly I met threading.BoundedSemaphore() , what is the difference between these? such as the following code snippet (to apply limitation on threads): import threading sem = threading.Semaphore(5) sem = threading.BoundedSemaphore(5) 回答1: A Semaphore can be released more times than it's acquired, and that will raise its counter above the starting value. A