Questions tagged [synchronized]
A block or method is said to be 'synchronized' if entry to it is controlled by the Java `synchronized` keyword. This causes access to it to be restricted to a single thread at a time: when concurrent accesses occur, they are sequentialized in an unspecified order.
1,821
questions
0
votes
1
answer
41
views
why need to synchronized on class
I have the following example which tries to transfer money between 2 account, I was thinking a synchronized on this is enough, I ran the example several time and found the total is incorrect:
public ...
0
votes
0
answers
52
views
How to prevent race condition for specific case?
I'm trying to prevent race condition and also want to don't affect system performance as could as possible.
If two signal(data) arrive to server at the same time, handler starting to process these ...
-1
votes
1
answer
46
views
How to use synchronized method to prevent race condition in if statement?
I'm trying to prevent race condition in my code,
At one code block if statement checking a condition with a method (if a variable is null or not), and end of the code block (after some major ...
0
votes
1
answer
12
views
Multithreading Only Allow Reading Same Time Only Block Writing
I have a List<T> that multiple threads read from. Rarely some threads add or delete to that list. I know threads must use that object in a synchronized block. But in this way every thread should ...
3
votes
3
answers
155
views
How to dynamically add items to a PowerShell ArrayList and process them recursively using Runspace pool?
I have a for loop that iterates through an ArrayList and during the process, adds more items to the list and processes them as well (iteratively). I am trying to convert this function to run ...
0
votes
1
answer
62
views
How to synchronize a method in javascript?
I am trying to synchronise a singleton.
I would need to make this method like the equivalent of synchronized in java.
What happens to me is that because the socket takes a while, if the first two ...
0
votes
0
answers
33
views
Simple ludo game implementation with java threads
I'm new with Java threads and I am having a few problems. I'm trying to make a simple ludo game, where each player makes their turn in a new thread. While one player is performing their turn the other ...
1
vote
1
answer
57
views
Java "synchronized" keyword doesn't forbid other threads from changing class member's internal elements
I'm referring to JCP 4.4.2 and it has following sample code
@ThreadSafe
public class ImprovedList<T> implements List<T>{
private final List<T> list;
public ImprovedList(List&...
0
votes
0
answers
39
views
How can Java achieve to let execute a certain "synchronized" method by ONLY one thread at a time?
How can JVM allow this? I don't understand it.
My hypothesis is that the all threads that we create are actually behind one single Java thread that manage which and how the behind thread actually ...
0
votes
1
answer
65
views
Thread synchronizing - almost same expressions but different result
public class MythreadM {
public static void main(String[] args) {
// TODO Auto-generated method stub
Msg m = new Msg();
Mythread thread1 = new Mythread(m);
...
0
votes
0
answers
30
views
Develop a multi-threaded application. Don't use synchronized word [duplicate]
Free checkout. The fast food restaurant has several cash desks. Customers stand in line at a particular cash desk, but can move to another queue if the queue decreases or disappears.
In my code, I ...
1
vote
0
answers
39
views
What does synchronized do in this section?
I do not understand exactly what synchronized does in this code
fun getDatabase(context: Context): UserDatabase{
val tempInstance = INSTANCE
if(tempInstance != null){
...
1
vote
1
answer
22
views
Can I use getApplicationContext() object for synchronizing?
When I have a small single block of code that should not get executed in parallel in my application (eg. when an Activity gets killed and restarted when the user turns the device), would it be ...
0
votes
1
answer
102
views
how to synchronously execute workflow using Elsa workflow engine
Elsa workflow supports asynchronously execution of workflow, so we can't get the result on spot, workflow execution performed by a thread, and response back instantly. So after completing workflow ...
0
votes
1
answer
171
views
Execute the Flowable workflow by REST API call and get the result
I am starting and completing the Flowable process using a REST API call.
Then how can wait and get the final "ServiceTask" result send it back back to the caller?
basic-process.bpmn20.xml:
&...
0
votes
0
answers
42
views
fail synchronize time between two local computers
I have two computers connected with an ethernet cable. Computer 1 runs on Windows, computer 2 runs on Ubuntu. The goal is to synchronize the clock of the Ubuntu computer to the Windows computer. I am ...
1
vote
0
answers
37
views
unfair synchronization among threads
I have a code pattern like this:
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;
public class TestRunner {
public static final Object ...
1
vote
1
answer
39
views
Why does synchronized on the count itself do not work? [duplicate]
Why the synchronizing on the count does not make it safe?
class Increment implements Runnable{
static Integer count = new Integer(0);
public void run(){
for (int i = 0; i< ...
2
votes
1
answer
58
views
Is mutating two distinct part of an object in an unsynchronized way unsafe?
Lets say I have a relatively simple object with two properties :
@Data
public class MyObject {
public Integer a;
public Integer b;
}
can I safely mutate a in some thread and b in some other ...
0
votes
0
answers
36
views
Is synchronized function equivalent to synchronized([Class].this)] [duplicate]
Have a look at the example below where synchronized is used twice:
public class Demo {
private Double mass = null;
public synchronized void setMass(double mass) {
this.mass = mass;
}
...
0
votes
2
answers
77
views
Monitor/Java synchronized methods, how to schedule an activity in a non-blocking way?
So, basically the whole idea is that I have to simulate the entrance of a school. N students queue at four turnstiles and then they join a random classroom. When the first student joins that classroom ...
1
vote
1
answer
42
views
Java Synchronized on the same file, but not different
I have this static class:
public class FileManager{
....
public static void writeOnFile(String something, String filepath){
File f= new File(filepath);
// Append
...
0
votes
0
answers
58
views
Java synchronized methods/monitors, if a method acquires the lock, does it keep it until the method ends?
I'll try to be as short as possible. I'm studying monitors at university and we described a model to implement it through semaphores. The way we modeled it if a thread entered a method f from a ...
1
vote
3
answers
65
views
Why does adding a synchronized block around this code change program ouput?
class Demo extends Thread{
Test x;
String name;
String task;
public Demo(String name, String task, Test p){
this.name = name;
this.task = task;
x = p;
}
...
1
vote
2
answers
182
views
Synchronized lock by particular ID
I have a REST API which has one method M which does something.
Of course it's called by multiple threads sometimes simultaneously.
This method M has an input String businessID
(which comes from the ...
1
vote
0
answers
111
views
How to synchronize java streams
I have a mail service in which there are two entities: the message and the sender.
First of all, I set up a connection to my mail and am trying to pull out information about SMS. Separately in the ...
0
votes
0
answers
54
views
Synchronized charts with different amounts of data points using Apexcharts
As you can see below, I have two synchronized charts(in the same group) that have different amounts of data points(the first graph has twice as much data as the second graph). Although the first and ...
0
votes
1
answer
75
views
How can a Thread communicate with another Thread without getting the "current Thread is not owner" failure?
so I'm trying to rebuild the consumer/producer pattern in Java. I create 20 ListInserter(Producer) and 20 ListNibbler(Consumer), which share one List. They are created like first Inserter, then ...
0
votes
1
answer
45
views
Generating odd even numbers using 2 threads via wait notify?
I am trying to generate odd even numbers using 2 threads via wait notify.
But it is only printing 0.
Could someone please explain why it is so.
Below is my code:
package waitNotify2;
class Odd ...
0
votes
0
answers
49
views
C# - Call dll method synchronously and return Error if execution timeout
I'm trying to call a dll method in C#.
There's few methods in this SOME.DLL and only one method should be executed at a time.
Also, if dll method execution time is longer than maxWait,
I want to ...
0
votes
1
answer
49
views
How are Cold Streams able to work properly in a concurrent environment, while obeying "Item 79" in "Effective Java"?
In summary:
The cascade effect nature of the Cold Stream, from Inactive to Active, Is in itself an "alien" execution (alien to the reactive design) that MUST BE EXECUTED WITHIN THE ...
1
vote
0
answers
44
views
Is there an issue with the synchronized methods in monitor?
Im trying to write kind of "Merge Sort" using threads in Java. Basically Monitor class gets an array and creates an Array Stack which contains arrays of length 1.
Monitor has 3 functions ...
0
votes
0
answers
45
views
Java Synchronized Block/Monitor vs Binary Semaphore
I have read various posts on semaphore and how they are different from mutex. Between choosing a Binary semaphore and a synchronized block, what factors should I consider to make my decision?
Problem ...
0
votes
1
answer
50
views
Polygon.contains() throws NullPointerException
Some context, I'm developing an Asteroids game
So, my Asteroid is a Polygon (Java built-in object) and a bullet is simply a point.
When I'm trying to detect a collision between an asteroid and a ...
1
vote
1
answer
54
views
How to check for changes in a List in java?
I have a question, I want to know if it's possible to check continuously for adds to a list.
I have a node with a file to transfer, I connect it to a directory.
I have another node without the file ...
0
votes
0
answers
7
views
Vector vs synchronizedList
I have read about vector and synchronizedList. As per few suggestion here, vector is not fully thread-safe. It throws ConcurrentModificationException. so use synchronizedList.
But I have tried ...
1
vote
1
answer
62
views
Synchronized method does not work but synchronized block does, why?
public class driver{
public static void main(String[] args) {
PrintNum firstObjectForThread = new PrintNum(0); // the argument is not used, ignore it
firstObjectForThread.startNewThread();
...
0
votes
1
answer
55
views
Will synchronized block in constructor of singleton calls synchronise the threads in Java?
I was trying something crazy :p
Created a TestClass (following singleton design pattern)
A main method which initialise the reflection for TestClass and starts two thread which creates TestClass new ...
3
votes
1
answer
120
views
is synchronized needed in getValue() ? & volatile needed?
I've a class in multithreading application:
public class A {
private volatile int value = 0; // is volatile needed here?
synchronized public void increment() {
value++; ...
1
vote
1
answer
85
views
Java Synchronized account example isn't working as expected
I'm using Account example to practice the Java Synchronized.
And here is my code.
Account class
class Account {
public int dollars;
public Account(int d) {
dollars = d;
}
// might fail due to ...
0
votes
1
answer
51
views
Does the compareAndSet return value guarantees a "Happens before" relationship?
I am sure many questions have been asked on this subject, but I guess all of them have dodge me.
My question arises since I've looked variations of the same code, in which a spin lock is combined with ...
1
vote
1
answer
78
views
My java unit test failed if there is a call to wait method inside a synchronized method
I am learning multi-threads programming in java recently. And I don't understand why the following test case will fail. Any explanation will be much appreciated.
Here is MyCounter.java.
public class ...
0
votes
1
answer
71
views
Not reevaluating expensive data in different threads
i have such a method
public Object doSomethingExpensive(String x);
now if i processed this method i can save the result in a HashMap for example, they key is the String x and the value is the result ...
1
vote
0
answers
38
views
How to multi-thread parsing of JMS messages
In my Spring Boot project, I have two JMS listeners listening to one queue. All messages received from the queue have to be processed in the same way and persisted / updated in the database (Oracle). ...
0
votes
0
answers
39
views
How do I make a method atomic with System.out.print in it [duplicate]
I want to make the print method to do both system.out.print statements at the same time, i.e I want the console to print all the time:
Thread-0 playing:
Thread-0 stop playing
Thread-1 playing:
Thread-...
0
votes
1
answer
63
views
Is "double checked locking" broken here in java?
I find an example for double checked locking.
However, I think this example is invalid because it's possible that another thread may see a non-null reference to a DoorControlManage object of door 1 ...
0
votes
0
answers
159
views
Angular Chart.js synchronized crosshair unsynchronized tooltip
I have several Ng2 charts on a page, those implements chartjs-plugin-crosshair.
Because of this plugin I have a synchronised crosshair and zoom and that’s what I need, but I don't want to have tooltip ...
-1
votes
2
answers
74
views
Visibility of mutable object under different locks in Java
mFeaute is a mutable object.
I want to know if the change of mFeature in setFeature(Feature feature) is visible to mFeature in useFeature(...) with a different explicit form of synchronized.
Thanks.
...
0
votes
1
answer
56
views
synchronization usage java [duplicate]
If I delete "synchronized" , "Wait 3 seconds and exit" will not be printed.
But if I add "System.out.println(getStarted());" or "System.out.println(123);"... ...
1
vote
1
answer
102
views
Does synchronized block prevent other threads from making insertions into a synchronized list?
So I am working on a multithreaded java application and I had this question:
List<Integer> mlist = Collections.synchronizedList(new ArrayList<Integer>());
public void addStuff(int a){
...