thread-safety

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.

Thread safety issue

折月煮酒 提交于 2021-01-28 02:31:47
问题 I have a LinkedList with Objects, that I want to process. Objects get added to it from another thread, but only one Thread removes/reads from it. private LinkedList<MyObject> queue = new LinkedList<>(); new Thread() { @Override public void run() { while (!Thread.interrupted()) { if (!queue.isEmpty()) { MyObject first = queue.removeFirst(); // do sth.. } } } }.start(); In another Thread I add Objects to the queue queue.add(new MyObject()); Sometimes this code leads to an Exception though,

Can I use local variables inside a Parallel Foreach loop (without unintentionally rewriting the previous value)

情到浓时终转凉″ 提交于 2021-01-27 19:05:44
问题 So I am attempting to process records from a datatable one row at a time. I am new to multi-threaded environments and I was asked to use Parallel.Foreach Loop. I wanted to know how local variables are treated in a Parallel execution mode. Following is my code. Parallel.ForEach(dtReportData.Tables[0].AsEnumerable(), drow => { string strType = drow["type"]; //Based on this type, I am executing logic from a switch case. }); Now. I want to know if I can declare and assign value to the variable

Converting from a ForEach loop to a Parallel.ForEach loop when summarizing into a double slows things down

巧了我就是萌 提交于 2021-01-27 06:58:34
问题 I have a section of C# code as follows. This code summarizes a column of 'doubles' in a DataTable : var data = this.Db.ExecuteRead(query, this.Score.Name); var time = 0.0; foreach (DataRow row in data.Rows) { time += this.ParseDouble(row[0].ToString()) / MillisecondsPerMinute; } This code takes 4 seconds to execute. I wanted to speed it up, so I parallelized it as follows: Parallel.ForEach( data.AsEnumerable(), row => { time += this.ParseDouble(row[0].ToString()) / MillisecondsPerMinute; });

Converting from a ForEach loop to a Parallel.ForEach loop when summarizing into a double slows things down

拥有回忆 提交于 2021-01-27 06:57:23
问题 I have a section of C# code as follows. This code summarizes a column of 'doubles' in a DataTable : var data = this.Db.ExecuteRead(query, this.Score.Name); var time = 0.0; foreach (DataRow row in data.Rows) { time += this.ParseDouble(row[0].ToString()) / MillisecondsPerMinute; } This code takes 4 seconds to execute. I wanted to speed it up, so I parallelized it as follows: Parallel.ForEach( data.AsEnumerable(), row => { time += this.ParseDouble(row[0].ToString()) / MillisecondsPerMinute; });

Thread safe mersenne twister

孤街浪徒 提交于 2021-01-27 06:13:19
问题 Looking for a thread safe random generator I found a mersenne twister generator class that the author says if thread safe: http://www.umiacs.umd.edu/~yangcj/mtrnd.html But after studying the code I cannot see were it is safe thread. There are no locks of any kind or anything resembling a lock variable in there. Is this implementation really thread safe? If so what is the magic? 回答1: It appears to be thread-safe in the sense that two different MersenneTwist objects can be used concurrently.

Thread safe mersenne twister

半城伤御伤魂 提交于 2021-01-27 06:12:39
问题 Looking for a thread safe random generator I found a mersenne twister generator class that the author says if thread safe: http://www.umiacs.umd.edu/~yangcj/mtrnd.html But after studying the code I cannot see were it is safe thread. There are no locks of any kind or anything resembling a lock variable in there. Is this implementation really thread safe? If so what is the magic? 回答1: It appears to be thread-safe in the sense that two different MersenneTwist objects can be used concurrently.

Blocking Queue Take out of Order

狂风中的少年 提交于 2021-01-24 18:43:29
问题 I have a simple blocking queue containing Integers. In a multi thread environment I am taking and adding elements to the the back of the queue. BlockingQueue<Integer> test = new LinkedBlockingQueue<Integer>(); The goal is to have adding in order.. and taking be in the same order as well. On the 5th line of my program output, 3 is somehow at the front of the queue before 2 is, despite it appearing that 2 was added first. All of these were added in a single threaded environment, so I know the

Blocking Queue Take out of Order

最后都变了- 提交于 2021-01-24 18:28:23
问题 I have a simple blocking queue containing Integers. In a multi thread environment I am taking and adding elements to the the back of the queue. BlockingQueue<Integer> test = new LinkedBlockingQueue<Integer>(); The goal is to have adding in order.. and taking be in the same order as well. On the 5th line of my program output, 3 is somehow at the front of the queue before 2 is, despite it appearing that 2 was added first. All of these were added in a single threaded environment, so I know the

Blocking Queue Take out of Order

我的未来我决定 提交于 2021-01-24 18:27:22
问题 I have a simple blocking queue containing Integers. In a multi thread environment I am taking and adding elements to the the back of the queue. BlockingQueue<Integer> test = new LinkedBlockingQueue<Integer>(); The goal is to have adding in order.. and taking be in the same order as well. On the 5th line of my program output, 3 is somehow at the front of the queue before 2 is, despite it appearing that 2 was added first. All of these were added in a single threaded environment, so I know the