Linked Questions
54 questions linked to/from Difference between volatile and synchronized in Java
2 votes
5 answers
754 views
visibility of immutable object after publication
I have an immutable object, which is capsulated in class and is global state. Lets say i have 2 threads that get this state, execute myMethod(state) with it. And lets say thread1 finish first. It ...
2 votes
2 answers
263 views
Java Volatile ,synchronization,atomic example
Hi I am reading java concurrency in practice and I read interesting statement states that Locking can guarantee both visibility and atomicity; volatile variables can only guarantee visibility. ...
2 votes
2 answers
280 views
Will a variable be read "fresh from main memory" if used in a loop AROUND a synchronized lock?
Please see the code below: private static boolean flag=true; // main thread will call flag=false private final static Object lock=new Object(); // lock condition public static void thread1(){ ...
3 votes
1 answer
399 views
Does AtomicInteger handle synchronization?
If two threads both increment same int i using i++, we could get a problem, as i++ is not atomic operation. That is why there is AtomicInteger.increment(), which makes incrementing atomic. So if we ...
0 votes
1 answer
241 views
Volatile variable for read-write operations in Java
I'm learning about volatile and synchronized in Java and I see that synchronized is used for read-modify-write operations like x++, and volatile is for read-write operations. And I want to ask you 2 ...
1 vote
2 answers
144 views
Why am I seeing this weird output in Java threads in Socket connections?
So I have this little code: public class MyCounterClass { private static int counter = 0; synchronized public static int getCounter(){ return counter++; } } This is the server: ...
1 vote
2 answers
339 views
Java volatile keyword in multithreading
I tried to reproduce the non-volatile variable behavior in Java multi-threading. Here I have non-volatile variable test in OccurrenceCounter.java class. In ThreadDemo.java class I have main method ...
1 vote
2 answers
416 views
Why volatile is weaker form of synchronization?
As mentioned from Java_author, The Java language also provides an alternative, weaker form of synchronization, volatile variables, to ensure that updates to a variable are propagated predictably to ...
0 votes
4 answers
134 views
Volatile is better than locking in which cases?
In The Java Language Specification, Java SE 9 Edition there is a statement: The Java programming language allows threads to access shared variables (§17.1). As a rule, to ensure that shared variables ...
2 votes
1 answer
292 views
Does synchronized marks variable volatile automatically? [duplicate]
I was reading about synchronized and volatile in java. Each new article makes me confused. One article said "Java’s synchronized keyword guarantees both mutual exclusion and visibility". I am not sure ...
1 vote
3 answers
368 views
Why intrinsic lock object do not require special treatment (static, final, volatile)?
In this oracle example of intrinsic locks and many more, the monitor object is never declared as volatile, final or nor it has any distinction from any other regular object public class MsLunch { ...
1 vote
3 answers
70 views
Does jvm guarantie update of processor cache after switching threads?
I was asked by interviewer is there any danger not to use volatile if we know for sure that threads will never interfere. eg. we have: int i = 10; // Thread 1 i++; // await some time and switch ...
-1 votes
3 answers
162 views
Why do I need to use synchronized for multiple threads over volatile?
Some people says if multiple threads are reading/writing then you need to use synchronized and if one thread is reading/writing and another one is only reading then you must use volatile. I don't get ...
2 votes
3 answers
176 views
Is synchronization better option for multithreading shared resources?
public class MyResource { private int count = 0; void increment() { count++; } void insert() { // incrementing shared resource count for (int i = 0; i < 100000000; i++) { ...
0 votes
1 answer
170 views
Switching from Main thread to multiple Thread while analyzing a frame
I have been working on a Mobile application that analyzes the frame looking for specific objects. The processing was to heavy, and I keep getting 05-08 17:44:24.909: I/Choreographer(31606): Skipped ...