synchronization

questions around synchronization in java; when/how/to what extent

谁都会走 提交于 2019-12-12 16:40:33
问题 I am working on my first mutlithreaded program and got stuck about a couple of aspects of synchronization. I have gone over the multi-threading tutorial on oracle/sun homepage, as well as a number of questions here on SO, so I believe I have an idea of what synchronization is. However, as I mentioned there are a couple of aspects I am not quite sure how to figure out. I formulated them below in form of clear-cut question: Question 1: I have a singleton class that holds methods for checking

Looking for good analogy/examples for monitor verses semaphore

北城以北 提交于 2019-12-12 16:25:38
问题 A monitor is supposed to solve problems with semaphores in concurrent environments. I'm looking for a good analogy using a monitor verses semaphore. Please use information for the analogy: 4 tasks (TaskA, TaskB, TaskC, TaskD) 1 variable varX Each Task wants to manipulate varX based on some event. 回答1: Lets say a bunch of patients wants to go see a doctor. A semaphore implementation would be they all stand outside the door to the office, as soon as one patient comes out, they all try to

How to safely interrupt a thread with critial atomic logic?

…衆ロ難τιáo~ 提交于 2019-12-12 15:02:30
问题 I have a thread that does two stuff in a loop: wait for objects from a BlockingQueue and then process them. The processing logic has effects that is observable outside the JVM and must be done atomically. Currently this thread is controlled by a volatile boolean variable like discussed in https://stackoverflow.com/a/10961760/499922. However, this means the thread will not stop if there are no more messages on the queue for it to consume. 'Thread.interrupt()` cannot be used to resolve this

Semaphores makeWater() synchronization

為{幸葍}努か 提交于 2019-12-12 14:07:53
问题 This program claims to solve makeWater() synchronization problem. However, I could not understand how. I am new in semaphores. I would appriciate if you can help me to understand this code. 回答1: So you need to make H2O (2Hs and one O) combinations out of number of simultaneously running H-threads and O-threads. The thing is one 'O' needs two 'H' s. And no sharings between two different water molecules. So assume number of O and H threads start their processes. No O thread can go beyond P(o

fcntl() for thread or process synchronization?

一个人想着一个人 提交于 2019-12-12 14:07:43
问题 Is it possible to use fcntl() system call on a file to achieve thread/process synchronization (instead of semaphoress)? 回答1: Yes. Unix fcntl locks (and filesystem resources in general) are system-wide, so any two threads of execution (be they separate processes or not) can use them. Whether that's a good idea or not is context-dependent. 回答2: That's one way of synchronizing between processes, but if you don't want to use semaphores, you could use process shared mutexes, such as mutexes and

How to maintain mutable state in a Java Singleton

拜拜、爱过 提交于 2019-12-12 14:02:40
问题 I have a Singelton in Java (in an OSGi Service) and want to maintain some state in it (a counter). Should this variable be static? or synchronized? or both? Or should i wrap the actions in a syncronized method? (would this be any different than just making the var syncronized?) I want consumers of the service actions to increment this counter. public MyServiceImpl implements MyService { private int count = 0; // static? syncronized? public String helloWorld() { count++; return "Hello World";

Producer Consumer scenario with Reentrant Lock and Condition in Java

荒凉一梦 提交于 2019-12-12 13:16:07
问题 I have written a Producer Consumer program using Reentrant Lock and condition. It is working correctly, but I am not very sure whether implementation is correct. Moreover it does not seem to be optimal. Can somebody please verify if this is a correct implementation, moreover can you please tell, how to optimize it, like - taking lock at the place where it is really required public class TestRL { static class Observed { boolean filled = false; public void setFilled(boolean filled) { this

SymmetricDS Syncing is not syncing perfectly

為{幸葍}努か 提交于 2019-12-12 12:27:20
问题 I have a 3 server running SymmetricDS. Say i have node 1 which is master, node 2.node 3 are child. When some data is being inserted into node 2 it's being synced to node 1 and similarly node 3's data is being syncing with node 1. But node 2 data is not going to node 3. I do not know where to solve this issue. Here is the sample configuration sql -- ========================================= -- channel starts insert into sym_channel (channel_id, processing_order, max_batch_size, enabled,

Conditional reduction in CUDA

非 Y 不嫁゛ 提交于 2019-12-12 11:00:27
问题 I need to sum about 100000 values stored in an array, but with conditions. Is there a way to do that in CUDA to produce fast results? Can anyone post a small code to do that? 回答1: I think that, to perform conditional reduction, you can directly introduce the condition as a multiplication by 0 (false) or 1 (true) to the addends. In other words, suppose that the condition you would like to meet is that the addends be smaller than 10.f . In this case, borrowing the first code at Optimizing

Java: what happens when a new Thread is started from a synchronized block?

心不动则不痛 提交于 2019-12-12 10:39:41
问题 First question here: it is a very short yet fundamental thing in Java that I don't know... In the following case, is the run() method somehow executed with the lock that somemethod() did acquire? public synchronized void somemethod() { Thread t = new Thread( new Runnable() { void run() { ... <-- is a lock held here ? } } t.start(); ... (lengthy stuff performed here, keeping the lock held) ... } 回答1: No. run() starts in its own context, synchronization-wise. It doesn't hold any locks. If it