concurrency

Java - Immutable array thread-safety

蹲街弑〆低调 提交于 2020-01-01 03:04:27
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that

Java - Immutable array thread-safety

不问归期 提交于 2020-01-01 03:04:08
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that

How to make sure only one instance of a Bash script is running at a time?

给你一囗甜甜゛ 提交于 2020-01-01 02:34:30
问题 I want to make a sh script that will only run at most once at any point. Say, if I exec the script then I go to exec the script again, how do I make it so that if the first exec of the script is still working the second one will fail with an error. I.e. I need to check if the script is running elsewhere before doing anything. How would I go about doing this?? The script I have runs a long running process (i.e. runs forever). I wanted to use something like cron to call the script every 15mins

Immutable beans in Java

三世轮回 提交于 2020-01-01 01:13:10
问题 I am very curious about the possibility of providing immutability for java beans (by beans here I mean classes with an empty constructor providing getters and setters for members). Clearly these classes are not immutable and where they are used to transport values from the data layer this seems like a real problem. One approach to this problem has been mentioned here in StackOverflow called "Immutable object pattern in C#" where the object is frozen once fully built. I have an alternative

ConcurrentHashmap in JDK8 code explanation

南笙酒味 提交于 2019-12-31 22:40:14
问题 I have been trying to understand the ConcurrentHashMap functions in JDK8, in contrast of how it was in JDK7 (which, in addition to the source code, can be found explained quite well by some nice folks out there such as Richard http://www.burnison.ca/articles/the-concurrency-of-concurrenthashmap). It looks like have been changed quite a bit in JDK8 - e.g. there is no more 'segment' per se, but somehow I got a feeling that the changes are meant to make the code simpler? I'm having some

combine putIfAbsent and replace with ConcurrentMap

被刻印的时光 ゝ 提交于 2019-12-31 19:30:14
问题 I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation) I've the following code to offer: public void insertOrReplace(String key, String value) { boolean updated = false; do { String oldValue = concurrentMap.get(key); if (oldValue == null) { oldValue = concurrentMap.putIfAbsent

combine putIfAbsent and replace with ConcurrentMap

∥☆過路亽.° 提交于 2019-12-31 19:29:13
问题 I have a usecase where I have to insert a new value if the key does not exist in the ConcurrentHashMap replace the old value with a new value if the key already exists in the ConcurrentHashMap, where the new value is derived from the old value (not an expensive operation) I've the following code to offer: public void insertOrReplace(String key, String value) { boolean updated = false; do { String oldValue = concurrentMap.get(key); if (oldValue == null) { oldValue = concurrentMap.putIfAbsent

Trying to understand the Peterson's Algorithm

情到浓时终转凉″ 提交于 2019-12-31 18:53:41
问题 I am trying to understand the Peterson's algorithm and I came across this question. I traced the code and I wrote my observations. Please check my observation, Am I on the right track? Question from Textbook: Suppose the are only two processes, one with pid value 0 and one with pid value 1. What is wrong with this concurrency algorithm? while(True) { flag[pid] = 1 turn = 1-pid while( flag[1-pid] && turn == 1-pid ); // busy wait // critical section ... // end of critical section flag[pid] = 0

Trying to understand the Peterson's Algorithm

不想你离开。 提交于 2019-12-31 18:52:23
问题 I am trying to understand the Peterson's algorithm and I came across this question. I traced the code and I wrote my observations. Please check my observation, Am I on the right track? Question from Textbook: Suppose the are only two processes, one with pid value 0 and one with pid value 1. What is wrong with this concurrency algorithm? while(True) { flag[pid] = 1 turn = 1-pid while( flag[1-pid] && turn == 1-pid ); // busy wait // critical section ... // end of critical section flag[pid] = 0

Azure database concurrent usage issues

让人想犯罪 __ 提交于 2019-12-31 13:30:14
问题 Just wanted to run this by you all to see if there are any bright ideas as I have exhausted all of my ideas after an entire day, night and morning of searching. The issues we’re encountering invariably centre around database connectivity when under concurrent usage (selenium test), e.g. timeouts, dropped/closed connections, database server unreachable. The issue does seem to be restricted to Azure as we’re yet to encounter the issue locally even when running the same selenium test on the same