Linked Questions
54 questions linked to/from Difference between volatile and synchronized in Java
37 votes
6 answers
12k views
Volatile keyword in Java - Clarification [duplicate]
I am really confused about what I read about the applications of volatile keyword in java. Is the following statement correct? "a write to a volatile field happens before every subsequent read of the ...
0 votes
7 answers
351 views
Volatile and synchronized keywords in Java [duplicate]
Possible Duplicate: Difference between volatile and synchronized in JAVA (j2me) I am a bit confused with the 2 java keywords synchronized and volatile. To what i understand, since java is a ...
0 votes
2 answers
1k views
Volatile variable and synchronized setter and getter [duplicate]
I have thread class that has two variables var1 and var2 that might be accessed from different threads. Can we assume that using var2 via synchronized getter and setter is the same like using volatile ...
1 vote
1 answer
2k views
Java Volatile variable still causing race condition [duplicate]
I was running an experiment on different approaches to deal with race condition in multi-threaded java applications . Strategies like atomic variables, synchronize worked well , but I dont see the ...
-3 votes
1 answer
428 views
Difference between Volatile and synchronized [duplicate]
I have executed below piece of code expecting getting count as 20000. I have declared count as volatile, but the output is not proper all the time. package threading; public class Demo5 { ...
0 votes
1 answer
137 views
Threads changing the same Variable wont synchronize [duplicate]
My problem is that the code should increment a 1000 times and then output it. But sometimes a isn't 1000 at the end. public class Counter extends Thread { private static Integer a = 0; ...
-2 votes
2 answers
121 views
difference between the keywords volatile and syncronized [duplicate]
In which situation can volatile replace synchronized?Also how can the write operations on double and long become atomic after declaring them as volatile
0 votes
0 answers
52 views
Should a method be synchronized if it access a volatile variable? [duplicate]
I have a volatile field in a class, I have a method which modify this field, this method can be invoked by different threads. Must the method be synchronized or not ?
855 votes
26 answers
414k views
What is the volatile keyword useful for?
I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation. Volatile variables are a simpler -- but weaker -- form of synchronization than locking, which in ...
1159 votes
18 answers
602k views
What does 'synchronized' mean?
I have some questions regarding the usage and significance of the synchronized keyword. What is the significance of the synchronized keyword? When should methods be synchronized? What does it mean ...
288 votes
9 answers
183k views
Volatile vs Static in Java
Is it correct to say that static means one copy of the value for all objects and volatile means one copy of the value for all threads? Anyway a static variable value is also going to be one value ...
93 votes
7 answers
48k views
When exactly do you use the volatile keyword in Java? [duplicate]
I have read "When to use 'volatile' in Java?" but I'm still confused. How do I know when I should mark a variable volatile? What if I get it wrong, either omitting a volatile on something that needs ...
52 votes
13 answers
84k views
Tips to prevent deadlocks in java
I am studying java threads and deadlocks, I understand deadlock's examples but I wonder if there are general rules to follow to prevent it. My question is if there are rules or tips that can be ...
50 votes
2 answers
9k views
What is Clojure volatile?
There has been an addition in the recent Clojure 1.7 release : volatile! volatile is already used in many languages, including java, but what are the semantics in Clojure? What does it do? When is ...
32 votes
4 answers
22k views
Thread Caching and Java Memory model
I'm trying to understand the Java memory model and threads. As far as I understand, each thread has a local copy of the "main" memory. So if one thread tries to change an int variable, for ...