-4

Hi Stackoverflow Community

I came across the following for loop in the following Java documentation (Lock Objects) earlier today and was wondering whether anyone would be able to elaborate what the ";;" - loop condition means?

Here the code-snipped I am talking about (full code provided below):

public void run() {
    Random random = new Random();
    for (;;) {
       try {
            Thread.sleep(random.nextInt(10));
        } catch (InterruptedException e) {}
        bowee.bow(bower);
    }
}

Here the entire code example:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;

public class Safelock {
    static class Friend {
        private final String name;
        private final Lock lock = new ReentrantLock();

        public Friend(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public boolean impendingBow(Friend bower) {
            Boolean myLock = false;
            Boolean yourLock = false;
            try {
                myLock = lock.tryLock();
                yourLock = bower.lock.tryLock();
            } finally {
                if (! (myLock && yourLock)) {
                    if (myLock) {
                        lock.unlock();
                    }
                    if (yourLock) {
                        bower.lock.unlock();
                    }
                }
            }
            return myLock && yourLock;
        }
            
        public void bow(Friend bower) {
            if (impendingBow(bower)) {
                try {
                    System.out.format("%s: %s has"
                        + " bowed to me!%n", 
                        this.name, bower.getName());
                    bower.bowBack(this);
                } finally {
                    lock.unlock();
                    bower.lock.unlock();
                }
            } else {
                System.out.format("%s: %s started"
                    + " to bow to me, but saw that"
                    + " I was already bowing to"
                    + " him.%n",
                    this.name, bower.getName());
            }
        }

        public void bowBack(Friend bower) {
            System.out.format("%s: %s has" +
                " bowed back to me!%n",
                this.name, bower.getName());
        }
    }

    static class BowLoop implements Runnable {
        private Friend bower;
        private Friend bowee;

        public BowLoop(Friend bower, Friend bowee) {
            this.bower = bower;
            this.bowee = bowee;
        }
    
        public void run() {
            Random random = new Random();
            for (;;) {
                try {
                    Thread.sleep(random.nextInt(10));
                } catch (InterruptedException e) {}
                bowee.bow(bower);
            }
        }
    }
            

    public static void main(String[] args) {
        final Friend alphonse =
            new Friend("Alphonse");
        final Friend gaston =
            new Friend("Gaston");
        new Thread(new BowLoop(alphonse, gaston)).start();
        new Thread(new BowLoop(gaston, alphonse)).start();
    }
}
Mike Nedelko
  • 709
  • 1
  • 8
  • 28
  • 1
    I just means 'loop forever' – pvg Nov 19 '16 at 03:46
  • 1
    `for (;;)` is an infinite loop, exactly the same as `while (true)`. See this [answer](http://stackoverflow.com/questions/8880870/java-for-vs-whiletrue/8880898#8880898). – Óscar López Nov 19 '16 at 03:46
  • 1
    There is no ";;" condition. As many tutorials explain a `for` loop header consists of three parts delimited by a single `;`. So what could it mean when where are two of them and nothing else? – Tom Nov 19 '16 at 03:46
  • Sorry that was the c## dup, here's the correct one http://stackoverflow.com/questions/8880870/java-for-vs-whiletrue – baao Nov 19 '16 at 03:46
  • @Tom https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html – pvg Nov 19 '16 at 03:48
  • @pvg I know what a for loop is. – Tom Nov 19 '16 at 03:49
  • @Tom very well. – pvg Nov 19 '16 at 05:07

1 Answers1

0

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

The three expressions of the for loop are optional; an infinite loop can be created as follows:

// infinite loop
for ( ; ; ) {

    // your code goes here
}

Please at least google the documentation before posting.

Overt_Agent
  • 510
  • 6
  • 15