concurrency

Simplest and understandable example of volatile keyword in Java

会有一股神秘感。 提交于 2019-12-28 01:42:41
问题 I'm reading about volatile keyword in Java and completely understand the theory part of it. But, what I'm searching for is, a good case example, which shows what would happen if variable wasn't volatile and if it were. Below code snippet doesn't work as expected (taken from here): class Test extends Thread { boolean keepRunning = true; public void run() { while (keepRunning) { } System.out.println("Thread terminated."); } public static void main(String[] args) throws InterruptedException {

Java ReentrantReadWriteLocks - how to safely acquire write lock?

。_饼干妹妹 提交于 2019-12-27 18:23:01
问题 I am using in my code at the moment a ReentrantReadWriteLock to synchronize access over a tree-like structure. This structure is large, and read by many threads at once with occasional modifications to small parts of it - so it seems to fit the read-write idiom well. I understand that with this particular class, one cannot elevate a read lock to a write lock, so as per the Javadocs one must release the read lock before obtaining the write lock. I've used this pattern successfully in non

Java ReentrantReadWriteLocks - how to safely acquire write lock?

做~自己de王妃 提交于 2019-12-27 18:22:20
问题 I am using in my code at the moment a ReentrantReadWriteLock to synchronize access over a tree-like structure. This structure is large, and read by many threads at once with occasional modifications to small parts of it - so it seems to fit the read-write idiom well. I understand that with this particular class, one cannot elevate a read lock to a write lock, so as per the Javadocs one must release the read lock before obtaining the write lock. I've used this pattern successfully in non

Which concurrent Queue implementation should I use in Java?

我们两清 提交于 2019-12-27 12:07:59
问题 From the JavaDocs: A ConcurrentLinkedQueue is an appropriate choice when many threads will share access to a common collection. This queue does not permit null elements. ArrayBlockingQueue is a classic "bounded buffer", in which a fixed-sized array holds elements inserted by producers and extracted by consumers. This class supports an optional fairness policy for ordering waiting producer and consumer threads LinkedBlockingQueue typically have higher throughput than array-based queues but

Working with Slices and Golang sync.Map Structure

孤街浪徒 提交于 2019-12-25 19:37:49
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

末鹿安然 提交于 2019-12-25 19:37:33
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

☆樱花仙子☆ 提交于 2019-12-25 19:37:17
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Multiple threads arrive, the last should do the processing

浪子不回头ぞ 提交于 2019-12-25 18:50:41
问题 I'm implementing a logging where multiple threads can write into one List of log. The last thread should write the contents of the List to a file. So the thread which is the last to write into the List should flush the List into a file. What is the best way to accomplish this? For the List I just need one of the concurrent classes that is efficient for multiple writers and one reader. 回答1: In my case the simple solution was to implement Closeable and then do the flushing in the close method.

Multiple Runnable in a single thread -java

南笙酒味 提交于 2019-12-25 18:38:48
问题 I am trying to have a bunch of runnable threads that can be started one at a time. Something like First(new Thread() { public void run() { //do something } }); Is what I'm trying to do impossible? 回答1: You can use a single threaded Executor ExecutorService service = Executors.newSingleThreadedPool(); service.submit(runnable1); service.submit(runnable2); service.submit(runnable3); 回答2: Yes, just have multiple private methods: public class FirstCaller { private void method1() { } private void

How to pass parameters to a Thread object?

感情迁移 提交于 2019-12-25 17:37:49
问题 I'm working with a C++ class-library that provides a Thread base-class where the user has to implement a run() method. Is there a recommended way on how to pass parameters to that run() method? Right now I prefer to pass them via the constructor (as pointers). 回答1: I'm not sure about C++, but that's how you would do it in Java. You'd have a class that extends Thread (or implements Runnable) and a constructor with the parameters you'd like to pass. Then, when you create the new thread, you