Linked Questions

2 votes
5 answers
754 views

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 ...
dimitar's user avatar
  • 177
2 votes
2 answers
263 views

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. ...
so_what's user avatar
  • 176
2 votes
2 answers
280 views

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(){ ...
Qin Dong Liang's user avatar
3 votes
1 answer
399 views

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 ...
Ana Maria's user avatar
  • 531
0 votes
1 answer
241 views

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 ...
user avatar
1 vote
2 answers
144 views

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: ...
Koray Tugay's user avatar
1 vote
2 answers
339 views

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 ...
nayak0765's user avatar
  • 193
1 vote
2 answers
416 views

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 ...
overexchange's user avatar
  • 17.3k
0 votes
4 answers
134 views

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 ...
Evgeny Mamaev's user avatar
2 votes
1 answer
292 views

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 ...
Java Beginner's user avatar
1 vote
3 answers
368 views

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 { ...
Radu Ionescu's user avatar
  • 3,542
1 vote
3 answers
70 views

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 ...
Rudziankoŭ's user avatar
  • 11.3k
-1 votes
3 answers
162 views

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 ...
hellzone's user avatar
  • 5,376
2 votes
3 answers
176 views

public class MyResource { private int count = 0; void increment() { count++; } void insert() { // incrementing shared resource count for (int i = 0; i < 100000000; i++) { ...
Javed Solkar's user avatar
0 votes
1 answer
170 views

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 ...
Ignacio Ferrero's user avatar

15 30 50 per page