synchronization

wait(), notify() and notifyAll() inside synchronized statement

独自空忆成欢 提交于 2019-12-05 16:40:14
I get following error when trying to do invoke notifyAll() inside a synchronized statement: Invoked Object.notify() outside synchronized context. Example: final List list = new ArrayList(); synchronized(list) {..... invoked notifyAll() here}; You can only call wait() , notify() , and notifyAll() on the object that is being synchronized on: synchronized (list) { //... list.notifyAll(); } In other words, the calling thread must own the object's monitor. If, inside synchronized (list) , you call notifyAll() , you are actually calling notifyAll() on this rather than list . My guess is that you are

C# enforcing order of statement execution

时光毁灭记忆、已成空白 提交于 2019-12-05 16:29:15
问题 My question is about order of execution guarantees in C# (and presumably .Net in general). I give Java examples I know something about to compare with. For Java (from "Java Concurrency in Practice") There is no guarantee that operations in one thread will be performed in the order given by the program, as long as the reordering is not detectable from within that thread-even if the reordering is apparent to other threads. So the code y = 10; x = 5; a = b + 10; may actually assign a=b+10 Before

Synchronized data read/write to/from main memory

邮差的信 提交于 2019-12-05 16:10:54
When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the data it needs from main memory or will it clear all the member variables in the cache and read their values from main memory ? For example public class SharedData { int a; int b; int c; int d; public SharedData() { a = b = c = d = 10; } public synchronized void compute() { a = b * 20; b = a + 10; } public synchronized int getResult() { return b*c; } } In the above code assume compute is executed by

Python shared string memory for multiprocessing

不羁的心 提交于 2019-12-05 15:14:02
I'm trying to use a shared string variable between my Python processes, but it seems that I'm doing something wrong since I'm getting coredumps and invalid memory values. I use multiprocessing.Value to create a ctypes.c_char_p value and use the value attribute to access it. In my understanding of the Python docs the value attribute should be synchronized, as long it is an instance of Value (contrary of an instance of RawValue ). Is that correct so far? I've created a short example to demonstrate my use of Value and to show the inconsistency while executing: from multiprocessing import Process,

Native method synchronization

为君一笑 提交于 2019-12-05 15:00:03
I have a native method method defined like this: public static native int doSomething(); However, this method is not thread-safe. So, I put a synchronized keyword on it, so it looks like this now: public static synchronized native int doSomething(); This appears to fix the problem, but I'm not sure if it actually does. Is this valid? Does it actually properly lock access to the method? After reading the relevant JLS section , there is nothing in the JLS which prohibits static and native from being in the method definition. According to the relevant JVM spec: Method-level synchronization is

How to ensure Thread safety of subclass' methods from a superclass?

喜你入骨 提交于 2019-12-05 14:25:14
I attended an interview and I was asked to design a class for the following requirement. Assume I have a class A and it can have any number of children, i.e., subclasses. The class A has a method called doSomething() which is synchronized. The requirements are : It is mandatory that all subclasses of A override the doSomething() method. All subclasses' overriden doSomething() method must be Thread safe in nature. All subclasses' must have the provision to implement their own logic for their doSomething() method implementations. Class A's constructor is upto me (the designer) to decide how to

Force order of execution of C statements?

青春壹個敷衍的年華 提交于 2019-12-05 14:17:31
问题 I have a problem with the MS C compiler reordering certain statements, critical in a multithreading context, at high levels of optimization. I want to know how to force ordering in specific places while still using high levels of optimization. (At low levels of optimization, this compiler does not reorder statements) The following code: ChunkT* plog2sizeChunk=... SET_BUSY(plog2sizeChunk->pPoolAndBusyFlag); // set "busy" bit on this chunk of storage x = plog2sizeChunk->pNext; produces this:

Double checked locking with ConcurrentMap

北慕城南 提交于 2019-12-05 13:55:29
问题 I have a piece of code that can be executed by multiple threads that needs to perform an I/O-bound operation in order to initialize a shared resource that is stored in a ConcurrentMap . I need to make this code thread safe and avoid unnecessary calls to initialize the shared resource. Here's the buggy code: private ConcurrentMap<String, Resource> map; // ..... String key = "somekey"; Resource resource; if (map.containsKey(key)) { resource = map.get(key); } else { resource = getResource(key);

Spurious wakeups on windows. Is it possible?

自古美人都是妖i 提交于 2019-12-05 13:47:40
I recently learned "Spurious wakeups" Any people say that this problem possible only for some types of Linux PC. I use windows. I wrote test for Spurious wakeups. I got result that it is possible. But I want to show this test for you. Maybe I made mistake somewhere. my initial variant: import java.util.Random; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; public class TestSpuriousWakeups { static final int MAX_THREADS = 600; static final Object mutex = new Object(); static final CountDownLatch allThreadsStarted = new CountDownLatch(MAX_THREADS); static final

Extending HashMap<K,V> and synchronizing only puts

坚强是说给别人听的谎言 提交于 2019-12-05 13:26:47
I recently encountered a class on our code base that extends HashMap and synchronizes the put method. Aside from it being less efficient than using ConcurrentHashMap, what sort of problems may arise with extending HashMap and synchronizing only put(K,V)? Assume that we don't care whether get(K) returns the latest value or not(e.g. we are fine with threads overwriting each other and we don't care about the possible race conditions that may arise if the values of the map are used as locks themselves). Example: public class MyMap<K,V> extends HashMap<K,V> { //... public synchronized void put(K