synchronization

Correct way to synchronize ArrayList in java

不想你离开。 提交于 2019-11-26 09:33:50
问题 I\'m not sure if this is the correct way to synchronize my ArrayList . I have an ArrayList in_queue which is passed in from the registerInQueue function. ArrayList<Record> in_queue = null; public void registerInQueue(ArrayList in_queue) { this.in_queue = in_queue; } Now I\'m trying to synchronize it. Is this sychronizing my in_queue object correctly? List<Record> in_queue_list = Collections.synchronizedList(in_queue); synchronized (in_queue_list) { while (in_queue_list.size() > 0) { in_queue

WaitForSingleObject and WaitForMultipleObjects equivalent in Linux?

大兔子大兔子 提交于 2019-11-26 09:33:37
问题 I am migrating an applciation from windows to linux. I am facing problem with respect to WaitForSingleObject and WaitForMultipleObjects interfaces. In my application I spawn multiple threads where all threads wait for events from parent process or periodically run for every t seconds. I have checked pthread_cond_timedwait , but we have to specify absolute time for this. How can I implement this in Unix? 回答1: Stick to pthread_cond_timedwait and use clock_gettime . For example: struct timespec

Waiting on multiple threads to complete in Java

让人想犯罪 __ 提交于 2019-11-26 09:27:40
问题 During the course of my program execution, a number of threads are started. The amount of threads varies depending on user defined settings, but they are all executing the same method with different variables. In some situations, a clean up is required mid execution, part of this is stopping all the threads, I don\'t want them to stop immediately though, I just set a variable that they check for that terminates them. The problem is that it can be up to 1/2 second before the thread stops.

How does @synchronized lock/unlock in Objective-C?

青春壹個敷衍的年華 提交于 2019-11-26 09:25:06
Does @synchronized not use "lock" and "unlock" to achieve mutual exclusion? How does it do lock/unlock then? The output of the following program is only "Hello World". @interface MyLock: NSLock<NSLocking> @end @implementation MyLock - (id)init { return [super init]; } - (void)lock { NSLog(@"before lock"); [super lock]; NSLog(@"after lock"); } - (void)unlock { NSLog(@"before unlock"); [super unlock]; NSLog(@"after unlock"); } @end int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; MyLock *lock = [[MyLock new] autorelease]; @synchronized(lock)

Ensure that Spring Quartz job execution doesn&#39;t overlap

故事扮演 提交于 2019-11-26 09:18:55
问题 I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes it takes just few seconds to execute, but as data gets bigger I\'m sure it run for 20 seconds or more. How can I prevent Quartz from firing/triggering the job while one instance is still being executed? Firing 2 jobs performing same operations on a database would not be so good. Is there a way I can do some kind of synchronization? 回答1: If all you need to do is fire every 20 seconds, Quartz is serious overkill

Is it possible to use mutex in multiprocessing case on Linux/UNIX ?

折月煮酒 提交于 2019-11-26 08:55:57
问题 This is an interview question. Is it possible to use mutex in multiprocessing case on Linux/UNIX ? My idea: No, different processes have separate memory space. mutex is only used for multithreading. semaphore is used for multiprocessing to do synchronization. right ? Any comments are welcome. thanks 回答1: Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution

How to acquire a lock by a key

♀尐吖头ヾ 提交于 2019-11-26 08:25:36
问题 What is the best way to prevent concurrent update of one record in a key-value set without locking the entire set? Semantically, I\'m looking for some kind of locking by a key (ideally, Java implementation, but not necessarily): interface LockByKey { void lock(String key); // acquire an exclusive lock for a key void unlock(String key); // release lock for a key } This lock is intended to synchronize an access to a remote store, so some synchronized Java collection is not an option. 回答1: Guava

What are the differences between various threading synchronization options in C#?

a 夏天 提交于 2019-11-26 07:51:12
问题 Can someone explain the difference between: lock (someobject) {} Using Mutex Using Semaphore Using Monitor Using Other .Net synchronization classes I just can\'t figure it out. It seems to me the first two are the same? 回答1: Great question. I maybe wrong.. Let me try.. Revision#2 of my orig answer.. with a little bit of more understanding. Thanks for making me read :) lock(obj) is a CLR construct that for (intra-object?) thread synchronization. Ensures that only one thread can take ownership

Does lock() guarantee acquired in order requested?

纵然是瞬间 提交于 2019-11-26 07:30:08
问题 When multiple threads request a lock on the same object, does the CLR guarantee that the locks will be acquired in the order they were requested? I wrote up a test to see if this was true, and it seems to indicate yes, but I\'m not sure if this is definitive. class LockSequence { private static readonly object _lock = new object(); private static DateTime _dueTime; public static void Test() { var states = new List<State>(); _dueTime = DateTime.Now.AddSeconds(5); for (int i = 0; i < 10; i++) {

Lock (Monitor) internal implementation in .NET

£可爱£侵袭症+ 提交于 2019-11-26 06:39:23
问题 For mastering of some technology you have to know how it\'s made at one abstraction level lower. In case of multithreading programming, it will be good to know about synchronization primitives. Here is the question, how implemented Lock (Monitor) in .NET? I\'m intrested in such points: - does it utilize OS objects?; - does it require user mode or kernel mode?; - what is overhead for threads that are waiting for lock?; - in what cases threads queue that awaiting for the lock could be violated?