2

So I was using AtomicLong and decided to have a look at its implementation, and noticed the following difference between 2 methods:

getAndAdd(long delta):

public final long getAndAdd(long delta) {
    while (true) {
        long current = get();
        long next = current + delta;
        if (compareAndSet(current, next))
            return current;
    }
}

incrementAndGet():

public final long incrementAndGet() {
    for (;;) {
        long current = get();
        long next = current + 1;
        if (compareAndSet(current, next))
            return next;
    }
}

What struck me as odd is that both methods do almost exactly the same thing, but they were implemented using 2 different loops; a while in the first and a for in the second. As far as I can tell this would make no difference in performance. Is there a specific reason for this?

tmbrggmn
  • 8,680
  • 10
  • 35
  • 44
  • 7
    I think the reason is that different developers where involved. But both loops are exactly the same. – Stephan Jan 06 '12 at 13:21
  • 1
    I don't even see why they wouldn't compile to the same bytecode, either. I would need to test this to be sure, but I would expect that `while(true)` and `for(;;)` produce the same bytecode output. The only thing I can think of is @Stephan's suggestion of different developers, but I don't know how you'd prove or disprove that without access to the version control history for the file. – Thomas Owens Jan 06 '12 at 13:28
  • @Stephan & @Thomas: I had considered the possibility, but in the JDK6 source code I can only see Doug Lea as the `@author`, which made me extra curious as to why this has been implemented the way it was. Maybe Doug is just playing with us. – tmbrggmn Jan 06 '12 at 13:38
  • I'm e-mailing Doug right now to ask the question. We must know. – tmbrggmn Jan 06 '12 at 13:42
  • @Stephan was apparently right. After asking the man himself, he replied that he prefers the `for(;;) {}` syntax, but in this case he was collaborating with other people who apparently preferred the `while(true) {}` syntax. – tmbrggmn Jan 06 '12 at 14:00
  • @pHk You should post that as an answer and accept that instead of the currently selected answer. Paste quotes right from the email. – Thomas Owens Jan 06 '12 at 15:21

4 Answers4

3

No, there are just two ways of expressing infinite loop in Java. None is universally belived to be better.

zch
  • 14,931
  • 2
  • 41
  • 49
  • +1: Plus `do { /* */ } while(true);` Actually there is an infinite number of possible implementations of an infinite loop. ;) – Peter Lawrey Jan 06 '12 at 13:29
2

After asking the original author about why this was implemented in this way, this is what he replied:

I always use "for(;;)" (pronounced "forever"), except when collaborating (in this case with JVM folks) who for some stupid reason prefer "while(true)".

To summarize: there is no difference between for(;;) {} and while(true) {} and the reason why both are used interchangeably in AtomicLong is because different authors preferred different syntaxes.

tmbrggmn
  • 8,680
  • 10
  • 35
  • 44
1

You might want to check the produced bytecode. I wouldn't be surprised if it is identical.

Maybe they are from different authors, or different moods... When I write lots of for loops, I'd probably go with the for(;;) { syntax, when speding too much time writing while loops, I'd use while(true) {.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
1

There is no difference between while(true) and for(;;), both should produce the same bytecode (you can check this with javap). Some might say that you should use while(true) since for(;;) might confuse beginners, however that is more a matter of style.

josefx
  • 15,506
  • 6
  • 38
  • 63