Questions tagged [java-memory-model]

The Java Memory Model (JMM) describes which executions of a program are legal by determining what value(s) can be observed when reading a shared variable according to certain rules.

java-memory-model
Filter by
Sorted by
Tagged with
0 votes
2 answers
114 views

Happen before relationship to avaid deadlock

In some article i read if happen before relationship is maintained in a multithreading program in java, there is no chance of arising data race. But my doubt is will it help also to prevent deadlock ...
user2392631's user avatar
1 vote
2 answers
232 views

What does "The default initialization of any object happens-before any other actions (other than default- writes) of a program" mean?

Java Language Specification 7, section 17.4.5: 5. The default initialization of any object happens-before any other actions (other than default-writes) of a program. What does it exactly mean? What ...
ZhekaKozlov's user avatar
  • 38.3k
1 vote
1 answer
153 views

data race example of SPECIAL POPL ISSUE The Java Memory Model confusion

I am confused with Figure 3 in SPECIAL POPL ISSUE The Java Memory Model paper. Example (page 5, Figure 3): Initially, x == y == 0 Thread 1 -> r1 = x; y = r1; Thread 2 -> r2 = y; r3 = r2 |...
2dy's user avatar
  • 139
1 vote
2 answers
113 views

Is this way of passing objects between threads safe under JMM?

I'm looking for a safe way to pass an object from a background thread to the UI thread. Does the code below do it safely? // on background thread final HugeObject object = ...
Fixpoint's user avatar
  • 9,739
3 votes
2 answers
1k views

Synchronized and Code reorder in java

In some link it is given that code reorder inside synchronized block is possible, whereas some other site is telling not possible. Can u please give an example to describe what actually happens in ...
user2392631's user avatar
-2 votes
2 answers
1k views

if primitive data is part of object where it is stored in java memory? [duplicate]

I know Stack memory stores primitive types and the addresses of objects and the object values are stored in heap memory. But if primitive is part of object then where it will be stored in Heap or ...
user2510115's user avatar
8 votes
3 answers
448 views

What is the memory visibility of variables accessed in static singletons in Java?

I've seen this type of code a lot in projects, where the application wants a global data holder, so they use a static singleton that any thread can access. public class GlobalData { // Data-...
Matt Quigley's user avatar
  • 7,760
6 votes
4 answers
266 views

Thread-safe but fast access to an "eventually final" variable?

I have a server somewhat like this: class Server { private WorkingThing worker; public void init() { runInNewThread({ // this will take about a minute ...
Bart van Heukelom's user avatar
4 votes
1 answer
191 views

Valid reorderings - under new JMM

I just wanted to know whether the below reorderings are valid one or not under new JMM model Original Code: instanceVar1 = value ;// normal read operation, no volatile synchronized(this) {...
veritas's user avatar
  • 2,454
11 votes
4 answers
7k views

Is setting a HashMap thread safe?

I have a HashMap in my program which is accessed by multiple threads, and is occasionally set by a single thread. For example: Map<String, String> myMap = new HashMap<String, String>(); ...
CorayThan's user avatar
  • 17.5k
0 votes
3 answers
362 views

Does happen-before in Java new memory model also apply to the member of a object which is declared as volatile?

In the new Java memory model, any write to a variable is guaranteed to be completed before the next thread read it. I wonder if this is also the case for variables that are member of this object. ...
grumpynerd's user avatar
13 votes
1 answer
2k views

What is the scope of memory flushed or published to various threads when using volatile and synchronized?

This question is in reference to memory visibility only, not happens-before and happens-after. There are four ways in Java that guarantees changes to memory in one thread to be made visible to another ...
Matt Quigley's user avatar
  • 7,760
4 votes
1 answer
638 views

What makes immutable objects to be published without safe publication techniques?

What does it mean to say that immutable objects can be published even without resorting to safe publication idioms? I have read Java Concurrency in Practice (Chapter 3 , Sharing Objects) but still ...
Geek's user avatar
  • 26.8k
15 votes
4 answers
2k views

Is volatile read happens-before volatile write?

I try to understand why this example is a correctly synchronized program: a - volatile Thread1: x=a Thread2: a=5 Because there are conflicting accesses (there is a write to and read of a) so in ...
user2394937's user avatar
8 votes
5 answers
5k views

How to write a simple thread-safe class using a volatile variable?

I want to write a simple thread-safe class that could be used to set or get an Integer value. The easiest way is to use the synchronized keyword: public class MyIntegerHolder { private Integer ...
Mateusz Zakrzewski's user avatar
59 votes
1 answer
15k views

Instruction reordering & happens-before relationship [duplicate]

In the book Java Concurrency In Practice, we are told several time that the instructions of our program can be reordered, either by the compiler, by the JVM at runtime, or even by the processor. So we ...
Martin Pernollet's user avatar
2 votes
1 answer
2k views

Maximum BackStack size in android

I'm a new one to android development. I need to know the Maximum memory Size of BackStack in android And i want to know How many activities of Android Application can be stored in BackStack Thanks
Dharani Kumar's user avatar
0 votes
3 answers
504 views

Java Puzzler: busy wait threads stop working

This is some sort of a Java Puzzler, that I stumbled across and can't really explain. Maybe somebody can? The following program hangs after a short time. Sometimes after 2 outputs, sometimes after 80,...
cheppsn's user avatar
  • 212
210 votes
5 answers
51k views

Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed ...
Dog's user avatar
  • 7,767
2 votes
3 answers
891 views

How to test the backing array used in Java Sting?

While i was developing my Android application, my attention was caught by the backing array. Multiple strings can share the same char[] because strings are immutable. The substring(int) method ...
midnite's user avatar
  • 5,226
8 votes
4 answers
1k views

How can an immutable object, with non-final fields, be thread unsafe?

say we have this // This is trivially immutable. public class Foo { private String bar; public Foo(String bar) { this.bar = bar; } public String getBar() { return bar; ...
NimChimpsky's user avatar
  • 46.9k
51 votes
8 answers
14k views

Must all properties of an immutable object be final?

Must immutable objects have all properties be final? I would say they don't. But I don't know whether I am right or not.
DRastislav's user avatar
  • 1,892
1 vote
1 answer
756 views

Do EARs deployed on one instace of WAS share the same heap space

When a wesbpher application server is running. it basically is running one JVM. Now if I deploy two EAR projects on the WAS, do these two EARS share the same JVM. Yes they do I guess. But then my ...
Victor's user avatar
  • 16.9k
16 votes
5 answers
4k views

Interpretation of "program order rule" in Java concurrency

Program order rule states "Each action in a thread happens-before every action in that thread that comes later in the program order" 1.I read in another thread that an action is reads and writes to ...
Abs's user avatar
  • 2,284
1 vote
2 answers
1k views

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in my vector3 class

Ok so I get this null Pointer exception and neither myself nor my lecturer can figure it out. (he reckons its bad logic and I'm inclined to agree) I'm making my own 3d object and rendering it with ...
That Homeless Guy's user avatar
3 votes
2 answers
193 views

Java : How is memory usage of a program calculated by a java compiler?

I am solving this problem. It desires a memory limit of 50000bytes . So if I allocate a 2D array of int of size 1000 X 1000 , shouldn't it exceed the memory bounds ? PS : I saw this solution to the ...
Nikunj Banka's user avatar
  • 11.2k
2 votes
2 answers
137 views

Can the JVM optimize multiple reads from an array

Can the JVM optimize array reads when reading the same index multiple times? Consider the following: public static void foo(int[] array) { for (int i=0; i<array.length; i++) { int value1 ...
Steve Kuo's user avatar
  • 62.5k
1 vote
3 answers
3k views

OutOfMemoryError: Java heap space when running jruby on rails console but not using all the memory defined by -J-Xmx

I have a rails 3 app using jruby 1.7 and I'm trying to use the rails console on my local dev environment (OS X 10.8 with jdk 7) in a situation that would require a lot of memory. I run the rails ...
razenha's user avatar
  • 7,720
26 votes
2 answers
7k views

final vs volatile guaranntee w.rt to safe publication of objects

From the book Java concurrency in practice : To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A ...
Geek's user avatar
  • 26.8k
4 votes
2 answers
439 views

Is it possible to efficiently implement a seqlock in Java?

Another question made me wonder if the seqlock can be efficiently implemented with a volatile version counter in Java. Here's a prototypical implementation, for the case there will only ever be a ...
BeeOnRope's user avatar
  • 62.5k
14 votes
4 answers
1k views

Java concurrent visibility of primitive array writes

I recently found this gem in my code base: /** This class is used to "publish" changes to a non-volatile variable. * * Access to non-volatile and volatile variables cannot be reordered, * ...
Steven Schlansker's user avatar
42 votes
10 answers
3k views

Immutability and reordering

The code below (Java Concurrency in Practice listing 16.3) is not thread safe for obvious reasons: public class UnsafeLazyInitialization { private static Resource resource; public static ...
assylias's user avatar
  • 325k
3 votes
4 answers
2k views

Java Concurrency - Publishing Immutable Objects (Java Concurrency In Practice)

In Java Concurrency In Practice, the author stated that Immutable objects can be published through any mechanism Immutable objects can be used safely by any thread without additional synchronization, ...
ben ben's user avatar
  • 269
2 votes
0 answers
160 views

memory barrier example of JMM cookbook confusion

I am confused with this compiler insertion of barrier example in the JMM cookbook http://g.oswego.edu/dl/jmm/cookbook.html i = u (don't it involve a volatile load from u and normal store into i ? ) ...
porkchop's user avatar
  • 289
3 votes
1 answer
221 views

Is there a tool to determine whether a program is "correctly synchronized" as defined in JLS?

The Java Language Specification 7 (JLS7-17.4.5) defines a "correctly synchronized" program like this: "A program is correctly synchronized if and only if all sequentially consistent executions are ...
Frans Lundberg's user avatar
3 votes
2 answers
1k views

Synchronized and the scope of visibility

I've been reading up on Java concurrency and had forgot the fact that synchronization blocks in two threads using the same lock also affect the visibility of variables, even though they were not ...
StrangeLoop's user avatar
16 votes
3 answers
540 views

Why is this behavior allowed in the Java Memory Model?

Causality in JMM seems to be the most confusing part of it. I have a few questions regarding JMM causality, and allowed behaviors in concurrent programs. As I understand, the current JMM always ...
gaganbm's user avatar
  • 2,753
4 votes
1 answer
225 views

Is declaring two fields volatile sufficient?

I am writing a Java ME application now. As I know, it uses old Java Memory Model, because its features are restrictred to Java 1.3. The only guarantee about volatile keyword this model provides is ...
user avatar
5 votes
1 answer
2k views

What does it mean when we say that a particular data structure is cache friendly?

I often read that linked list data structure and its variant skiplists are cache friendly in parallel hardware. What does this mean ? Can some one please explain in an easy to understand way . Edit: ...
Geek's user avatar
  • 26.8k
2 votes
4 answers
553 views

Java synchronized effect on Double-checked locking?

I have read through different articles such as Double-checked locking: Clever, but broken and I understand the reason of why the following code is broken in multi-threading usage. class SomeClass { ...
enix0907's user avatar
5 votes
2 answers
906 views

Memory allocation in Java - Android

If I have: Bitmap bitmap = Bitmap.create(..); // instance a bitmap = Bitmap.create(...); // instance b bitmap = null; bitmap = Bitmap.create(...); // instance c bitmap.recycle(); bitmap = Bitmap....
StackOverflowed's user avatar
24 votes
5 answers
1k views

Detailed semantics of volatile regarding timeliness of visibility

Consider a volatile int sharedVar. We know that the JLS gives us the following guarantees: every action of a writing thread w preceding its write of value i to sharedVar in program order happens-...
Marko Topolnik's user avatar
5 votes
4 answers
3k views

Synchronized data read/write to/from main memory

When a synchronized method is completed, will it push only the data modified by it to main memory, or all the member variables, similarly when a synchronized method executes, will it read only the ...
Karthik's user avatar
  • 345
16 votes
3 answers
5k views

Are hoisting and reordering the same thing?

I read from Effective Java that In the absence of synchronization the following sequence A below can be converted into sequence B by the virtual machine and this is called hoisting. I also read ...
Inquisitive's user avatar
  • 7,656
21 votes
2 answers
3k views

Please explain initialization safety as spelled out in Java memory model

Can some one explain initialization safety as required by Java memory model ? How does the final fields help in achieving initialization safety ? What role does the constructor play in ensuring ...
Inquisitive's user avatar
  • 7,656
7 votes
1 answer
2k views

Command pattern to pass application's activity methods?

After reading the the Avoiding memory leaks article by @RomainGuy I realized that my current Android application is plagued with the mistake of passing the application's main activity around. So ...
ih8ie8's user avatar
  • 954
17 votes
3 answers
3k views

java: `volatile` private fields with getters and setters

Should we declare the private fields as volatile if the instanced are used in multiple threads? In Effective Java, there is an example where the code doesn't work without volatile: import java.util....
khachik's user avatar
  • 28.5k
0 votes
2 answers
503 views

How to Ensure Memory Visibility in Java when passing data across threads

I have a producer consumer like pattern where some threads are creating data and periodically passing putting chunks of that data to be consumed by some other threads. Keeping the Java Memory Model ...
pdeva's user avatar
  • 44.5k
1 vote
1 answer
8k views

How much -XX:MaxPermSize size i can mention for 4GB and 8GB Ram and calculation for this?

How much -XX:MaxPermSize size i can mention for 4GB and 8GB Ram. Here are the other detalis of my system OS:-window XP(32 bit) RAM:-4 GB java_opt- -Xms1536m -Xmx1536m //(mentioned as environment ...
M Sach's user avatar
  • 33.8k
14 votes
3 answers
2k views

Effectively Immutable Object

I want to make sure that I correctly understand the 'Effectively Immutable Objects' behavior according to Java Memory Model. Let's say we have a mutable class which we want to publish as an ...
Volodymyr Sorokin's user avatar

1
4 5 6
7
8