concurrency

Ensure to get all values of `ConcurrentHashMap` when iterating over it while other threads put elements

帅比萌擦擦* 提交于 2021-01-28 08:16:18
问题 I have a ConcurrentHashMap that gets filled with put by 8 different Threads. One of the 8 Threads tries to read with a forEach consumer. My problem is, that the ConcurrentHashMap only has 5-7 entries. map.put(myContent); ... map.forEach(element -> ... do something); If I add a map.size() it for some reason shows all 8 entries map.put(myContent); map.size(); ... map.forEach(element -> ... do something); Going through the ConcurrentHashMap docs shows that it is not really thread-safe to iterate

Use of a key to synchronize access to code block

巧了我就是萌 提交于 2021-01-28 07:05:22
问题 Normally I would lock on a critical section like the following. public class Cache { private Object lockObject = new Object(); public Object getFromCache(String key) { synchronized(lockObject) { if (cache.containsKey(key)) { // key found in cache - return cache value } else { // retrieve cache value from source, cache it and return } } } } The idea being I avoid a race condition which could result in the data source being hit multiple times and the key being added to the cache multiple times.

How can I lock the internals of my Rust data structure?

百般思念 提交于 2021-01-28 04:35:38
问题 I'm trying to implement a collection that stores values in both a vector and a hashmap and this is what I have so far: pub struct CollectionWrapper { items: Vec<Item>, items_map: HashMap<ItemKey, Item>, } impl CollectionWrapper { pub fn new() -> Self { CollectionWrapper { items: Vec::new(), items_map: HashMap::new(), } } pub fn add(&mut self, item: Item) { let key = item.get_key(); self.items.push(item.clone()); self.items_map.insert(key, item.clone()); } } I obviously need some kind of lock.

How can I lock the internals of my Rust data structure?

邮差的信 提交于 2021-01-28 04:31:29
问题 I'm trying to implement a collection that stores values in both a vector and a hashmap and this is what I have so far: pub struct CollectionWrapper { items: Vec<Item>, items_map: HashMap<ItemKey, Item>, } impl CollectionWrapper { pub fn new() -> Self { CollectionWrapper { items: Vec::new(), items_map: HashMap::new(), } } pub fn add(&mut self, item: Item) { let key = item.get_key(); self.items.push(item.clone()); self.items_map.insert(key, item.clone()); } } I obviously need some kind of lock.

Java JIT compiler optimizations - is JIT consistent in respect to volatile variables value caching?

泪湿孤枕 提交于 2021-01-28 04:23:35
问题 I'm trying to better understand how does the JIT compiler work for java in respect to volatile variable value caching. Consider the example presented in this question: Infinite loop problem with while loop and threading: boolean loaded = false; // not volatile!!! private boolean loadAsset() { new Thread(new Runnable() { @Override public void run() { // Do something loaded = true; } }).start(); while (!loaded) { System.out.println("Not Loaded"); } System.out.println("Loaded"); return false; }

Adding and removing elements of a ConcurrentBag during enumeration

孤人 提交于 2021-01-28 02:51:10
问题 What happens when a thread is adding or removing elements of a ConcurrentBag<T> while another thread is enumerating this bag? Will the new elements also show up in the enumeration and will the removed elements not show up? 回答1: One can read the fine manual to discover: ConcurrentBag<T>.GetEnumerator Method The enumeration represents a moment-in-time snapshot of the contents of the bag. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to

Adding and removing elements of a ConcurrentBag during enumeration

筅森魡賤 提交于 2021-01-28 00:00:54
问题 What happens when a thread is adding or removing elements of a ConcurrentBag<T> while another thread is enumerating this bag? Will the new elements also show up in the enumeration and will the removed elements not show up? 回答1: One can read the fine manual to discover: ConcurrentBag<T>.GetEnumerator Method The enumeration represents a moment-in-time snapshot of the contents of the bag. It does not reflect any updates to the collection after GetEnumerator was called. The enumerator is safe to

Ember concurrency timeout hanging in qunit

我是研究僧i 提交于 2021-01-27 20:08:46
问题 In Ember I have a component that starts a never-ending poll to keep some data up to date. Like so: export default Component.extend({ pollTask: task(function * () { while(true) { yield timeout(this.get('pollRate')); this.fetchSomeData(); } }).on('init') }) This causes a preexisting acceptance test to get stuck in this task and run forever, even though it should be run asynchronously. The test looks like this: test('my test', async function(assert) { mockFindRecord(make('fetched-model')); await

Django reproduce concurrency example with select_for_update()

好久不见. 提交于 2021-01-27 19:39:12
问题 I'm reading over this article on handling concurrency in django The post talk about the following example: User A fetches the account — balance is 100$. User B fetches the account — balance is 100$. User B withdraws 30$ — balance is updated to 100$ — 30$ = 70$. User A deposits 50$ — balance is updated to 100$ + 50$ = 150$. I tried to reproduce with a UserBalance model with balance column that use IntegerField(), as the article said i need to use select_for_update() to avoid this issue def

Django reproduce concurrency example with select_for_update()

瘦欲@ 提交于 2021-01-27 19:20:36
问题 I'm reading over this article on handling concurrency in django The post talk about the following example: User A fetches the account — balance is 100$. User B fetches the account — balance is 100$. User B withdraws 30$ — balance is updated to 100$ — 30$ = 70$. User A deposits 50$ — balance is updated to 100$ + 50$ = 150$. I tried to reproduce with a UserBalance model with balance column that use IntegerField(), as the article said i need to use select_for_update() to avoid this issue def