2016-11-19 6 views
-4

Hallo Stackoverflow GemeinschaftPeculiar for-Schleife - for (;;) {} - in Java

stieß ich auf die folgende for-Schleife in der folgenden Java-Dokumentation (Lock Objects) heute früher und habe mich gefragt, ob jemand in der Lage wäre, erarbeiten was das ";;" - Schleifenbedingung bedeutet?

Hier ist der Code-snipped ich rede (full-Code zur Verfügung gestellt unten):

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

Hier wird das gesamte Codebeispiel:

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(); 
 
    } 
 
}

+1

Ich meine nur "Schleife für immer" – pvg

+1

'für (;;)' ist eine Endlosschleife, genau das gleiche wie 'while (true)'. Siehe hierzu [Antwort] (http://stackoverflow.com/questions/8880870/java-for-vs-whiteretrue/8880898#8880898). –

+1

Es gibt kein ";;" Bedingung. Wie viele Tutorials erklären, besteht ein 'for' Loop-Header aus drei Teilen, die durch ein einzelnes'; '. Also was könnte es bedeuten, wenn wo sind zwei von ihnen und nichts anderes? – Tom

Antwort

0

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

Die drei Ausdrücke der for-Schleife sind optional; eine Endlosschleife kann wie folgt erstellt werden:

// infinite loop 
for (; ;) { 

    // your code goes here 
} 

Bitte mindestens die Dokumentation Google vor der Veröffentlichung.

+6

Bitte schauen Sie mindestens nach einem Duplikat, bevor Sie antworten. –

+0

Ich nehme an, dass ich das verdient habe. –

+0

Hallo @Overt_Agent. Danke dafür. Ich habe es tatsächlich googelt und konnte keine definitiven Ressourcen finden. Deshalb schätze die Info :). Vielen Dank. – Z101