File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is a single Java statement Atomic ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is a single Java statement Atomic ?" Watch "Is a single Java statement Atomic ?" New topic
Author

Is a single Java statement Atomic ?

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Hello savvy pals,

Assertion :- Any java statement would ultimately be converted into Assembly language code (or Machine code finally) before it is executed. Moreover any java statement when converted would generally be translated into many Assembly language statements. i.e. 1 HLL statement == Many Assembly lang statmets. Knowing this....

Problem:- Is a single java statement atomic ???

if the answer to above question is Yess then

Asserion2 :- any looping or conditional construct is a java statement.

Problem2 :- But we need to synchronize a code having just one for loop. ???


Thanks !!!


SCJP 6.0 96%

(Connecting the Dots ....)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Most operations are indeed atomic. Operations involving 64 bit structures like double / long are not atomic by themselves. The 64 bit operation can be split into 2 32 bit operations. It is possible to see a weird result when only one of them has completed

That said you will not be quizzed about this in the SCJP. Promise

Here is a reference to the problem -> http://java.sun.com/docs/books/jls/third_edition/html/memory.html

Look for "Non-atomic Treatment of double and long". You can get around it by making a long / double volatile or creating memory barriers to the main memory using the synchronized keyword


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42908
    
  68
Deepak Bala wrote:Most operations are indeed atomic. Operations involving 64 bit structures like double / long are not atomic by themselves.

This sounds as if operations on 32 bit structures were atomic, which is not the case. Something as simple as "c++" (with "c" being an int) is not atomic. See http://java.sun.com/docs/books/tutorial/essential/concurrency/interfere.html and http://java.sun.com/docs/books/tutorial/essential/concurrency/atomicvars.html for details and workarounds.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Thanks for adding more clarity Ulf. I wanted to avoid mentioning classes that support Atomic operations since it might add more clutter to the OPs question.

Sahil the bottom line is that Concurrency topics for the SCJP will not try to quiz you in such detail.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@ Deepak and ulf

Thankyou so much

Atleast i got an issue to read further after SCJP !!!
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I'm speculating here, but it seems that a conditional test such
as x == y, excluding longs and doubles, would be atomic, but
any evaluations that come before would be a different atom.
Further, a context switch could occur before execution of the
first statement after the branch.

Jim ... ...


BEE MBA PMP SCJP-6
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Jim Hoglund wrote:I'm speculating here, but it seems that a conditional test such
as x == y, excluding longs and doubles, would be atomic, but
any evaluations that come before would be a different atom.
Further, a context switch could occur before execution of the
first statement after the branch.

Jim ... ...


I am not sure I quite understood that. Are you asking if the value of x or y can change while the comparison operation takes place ? Or are you saying the result of the comparison operation is assigned atomically.

but any evaluations that come before would be a different atom


Did you mean a different transaction ?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
I'm thinking that x and/or y could be the result of evaluating an expression and
these evaluations would not be atomic with the equality compare. Then x == y
is atomic but assigning its result, or branching on its result, would be interruptable.
Does this make sense?

Jim ... ...
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I agree with the above posts by Jim And Ulf. Going back to the original post I would add:
Sahil Kapoor wrote:Problem:- Is a single java statement atomic ???

In general, no. Some are, but many are not. You should never assume that a statement is atomic - you should know why that particular statement is atomic, or you should assume it isn't.

Sahil Kapoor wrote:Problem2 :- But we need to synchronize a code having just one for loop. ???

Sometimes, yes - even if all the individual statements in the loop are atomic. Because if you have two or more atomic statements, stuff can still happen in between those two statements. That's why even with a Vector or Collections.synchronizedList(), you can't safely iterate the list without additional synchronization. It's pretty difficult to do anything useful with one of these without additional synchronization, really. Which is why I consider them inherently evil, as they just mislead people into thinking that they're "thread-safe" just because they're "synchronized". To use synchronization effectively, you usually need to make sure the synchronization wraps around whatever group of statements you need to be protected from interference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is a single Java statement Atomic ?