This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes To the author: Memory model Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "To the author: Memory model" Watch "To the author: Memory model" New topic
Author

To the author: Memory model

Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi Cay,

I just checkde that Tiger has added JSR-133 for new memory model, could you briefly discribed what memory structure Tiger is using? Any brenchmark data for comparing the old model with the new one?

Thanks.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
The "memory model" is a rather specialized issue that is important in the following circumstances:

1) You have a computer with more than one CPU
2) Your Java program has multiple threads
3) Your threads simultaneously access the same memory values

Funny things can happen in these circumstances because each processor has its own cache. (RAM is quite slow compared to a modern CPU, so the CPU caches frequently accessed memory locations.) Each processor might use a different value for what should be the same memory location.

Also, the JIT compiler may reorder instructions in one thread, which may mess up assumptions about instruction order that another thread makes. For example, if one thread never modifies a variable, the compiler may only read it from memory once. Then the other thread's modification of the same variable becomes invisible.

The memory model gives precise rules when the JVM needs to ensure that variables are properly shared between threads (e.g. in a synchronized block or if the variables are volatile), and when the processor and the compiler are free to carry out optimizations.

It is probably the dullest JSR out there, but of course, this is important stuff, and I am glad that some people worry about it.

Application programmers probably shouldn't worry about it too much. My advice is:
1) Try to use the new threadsafe collections in JDK 5.0. The implementors
have done all the thread-safety grunt work for you.
2) If there are no collections that work for your task, use synchronization or the new JDK 5.0 locks and conditions
3) If that's too much trouble (e.g. to access a single shared int), use volatile

Cheers,

Cay


Author of Java 8 for the Really Impatient
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Thanks Cay.

That is to say, in case my applications are distributed and multithreaded, the JVM will cater all memory issues for me, and what I need to do is to make sure that the Objects used in the system is thread-safe?

For non-multithreaded applications, are there any impact on them?

Recently, I havent developed any non-Web-based applications, and I guess the multithreading issues are handled by the container, in case, J2EE Tiger released, which I suppose it will use the same memory model, the container might have less effort for handling these issues?

Nick
Cay Horstmann
author
Ranch Hand

Joined: Nov 14, 2004
Posts: 114
    
  10
Absolutely... Ideally you shouldn't have to worry much about it, particularly if you can have the container handle transactions. The people who implement the container will really appreciate the new multithreading features.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Thanks Cay.

Did you have any reference/articles about this area?

Nick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To the author: Memory model
 
Similar Threads
Java 5 final is final no more
Favorite animal in the zoo
differentiate plz
New K&B book - Aug 2005
WA #1.....word association