Linked Questions

37 votes
6 answers
12k views

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 ...
smatter's user avatar
  • 29.4k
0 votes
7 answers
351 views

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 ...
user1315906's user avatar
  • 3,532
0 votes
2 answers
1k views

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 ...
vico's user avatar
  • 18.5k
1 vote
1 answer
2k views

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 ...
nullPointerException's user avatar
-3 votes
1 answer
428 views

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 { ...
Anurag Kumar Pandey's user avatar
0 votes
1 answer
137 views

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; ...
nordlad's user avatar
  • 61
-2 votes
2 answers
121 views

In which situation can volatile replace synchronized?Also how can the write operations on double and long become atomic after declaring them as volatile
javarulz's user avatar
0 votes
0 answers
52 views

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 ?
Aleks 's user avatar
  • 121
855 votes
26 answers
414k views

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 ...
Richard's user avatar
  • 10.6k
1159 votes
18 answers
602k views

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 ...
Johanna's user avatar
  • 27.7k
288 votes
9 answers
183k views

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 ...
Jothi's user avatar
  • 15.2k
93 votes
7 answers
48k views

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 ...
Ricket's user avatar
  • 34.3k
52 votes
13 answers
84k views

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 ...
iberck's user avatar
  • 2,440
50 votes
2 answers
9k views

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 ...
nha's user avatar
  • 18.1k
32 votes
4 answers
22k views

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 ...
user1409534's user avatar
  • 2,314

15 30 50 per page