thread-synchronization

How Synchronization works in Java?

烈酒焚心 提交于 2019-12-17 18:28:55
问题 I have a doubt regarding Java Synchronization . I want to know if I have three Synchronized methods in my class and a thread acquires lock in one synchronized method other two will be locked ? I am asking this question because I am confused with the following statement . While a thread is inside a synchronized method of an object, all other threads that wish to execute this synchronized method or any other synchronized method of the object will have to wait. This restriction does not apply to

Java memory model: volatile variables and happens-before

假装没事ソ 提交于 2019-12-17 09:19:11
问题 I'd like to clarify how happens-before relation works with volatile variables. Let we have the following variables: public static int i, iDst, vDst; public static volatile int v; and thread A: i = 1; v = 2; and thread B: vDst = v; iDst = i; Are the following statements correct in accordance with Java memory model (JMM)? If not, what would be correct interpretation? i = 1 always happens-before v = 2 v = 2 happens-before vDst = v in JMM only if it's actually happens before in time i = 1 happens

How can I allow thread 2 to communicate on the port I opened in thread 1?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 04:35:02
问题 I'm trying to communicate with the same port using two different threads in a CLI C++ program (running on Windows but not using the Windows API). The port is a USB port (that leads to a converter to RS-232 and then to another device). I can successfully establish port communications in my main thread using OpenCom() and Transmit() , functions provided in a DLL by the manufacturer of those devices. I then call newThreadFunc() (defined in another DLL), which runs in a separate thread (let's

How to update a java swing gui witha server from another thread?

▼魔方 西西 提交于 2019-12-12 13:09:48
问题 I have this Java Swing application that starts a new thread that uses a executor pool to open a socket server every time a incomming client tries to establish a connection. The application need two buttons, one to start and another to stop the server. What I want is to show the server status, and disable the opposed button until its status changes. This is what I have by now, but I don't know how could I communicate with the EDT when the thread stops. What I can do is just check the isRunning

Synchronization mechanism for an observable object

偶尔善良 提交于 2019-12-12 08:18:26
问题 Let's imagine we have to synchronize read/write access to shared resources. Multiple threads will access that resource both in read and writing (most of times for reading, sometimes for writing). Let's assume also that each write will always trigger a read operation (object is observable). For this example I'll imagine a class like this (forgive syntax and style, it's just for illustration purposes): class Container { public ObservableCollection<Operand> Operands; public ObservableCollection

Does ConcurrentHashMap need synchronization when incrementing its values?

北城以北 提交于 2019-12-12 03:54:39
问题 I know ConcurrentHashMap is thread-safe e.g.putIfAbsent,Replace etc., but I was wondering, is a block of code like the one below safe? if (accumulator.containsKey(key)) { //accumulator is a ConcurrentHashMap accumulator.put(key, accumulator.get(key)+1); } else { accumulator.put(key, 0); } Keep in mind that the accumulator value for a key may be asked by two different threads simultaneously, which would cause a problem in a normal HashMap. So do I need something like this? ConcurrentHashMap

perform functions synchronously

对着背影说爱祢 提交于 2019-12-12 03:46:25
问题 i have a few functions that have to be implemented synchronously for 3 seconds. i have a sound being played using soundpool, a vibration pattern, and a background animation. all three of them work fine separately. but i am not able to time them properly so that all start together. these 3 functions need to be called repeatedly after every 15 seconds. and will be stopped on a button click. i tried using something like this, but it doesnt work properly. myThread = new Thread(new Runnable(){

java thread communication, independent file reading and wiriting

这一生的挚爱 提交于 2019-12-11 20:12:40
问题 Java. I have two threads. one will be continuously monitoring for some events and based on the events, it will be updating (addition or deletion) a file. the other thread which is a timer task event, will be updating the same file in regular intervals of time. I want the threads to update the file when the other thread is not accessing the file . I couldn't use locks as file updating code part is independent for each thread and in different java classes . how can i do so? thanks in advance.

Why are two synchronized blocks acting like I've provided different monitor objects, when both monitor fields reference the same object?

谁说胖子不能爱 提交于 2019-12-11 09:58:54
问题 I have written a class with an internal private class extending Thread. My outer class starts an instance of this thread, and the thread accesses fields of the outer class within a loop. However, external agents may call methods over the outer class that modify the fields of the outer class. These methods must be synchronized with the loop inside the thread, so that modifications don't interfere with the loop. I had been synchronizing using a "synchronized" block on a field of the outer class

Thread Synchronization with IntentService

心不动则不痛 提交于 2019-12-11 02:38:29
问题 I'm trying to create an app that makes HTTP requests through an intentservice. I need the app to wait for the service to finish its run (aka, have the request be returned with some data) before it continues its operations, as its operations involve manipulation of the data I hope to receive from the HTTP requests. I've tried numerous means of doing so - Semaphore, CountDownLatch, but it seems that for all of them, I need some method of passing in the waiting/counting object into the