-1
while(true){//main loop
   while(){}//inner loop
   while(){} 
}

This is main program in raspberry pi. I want to get main loop when program execute in nested loop after time expire. The time out function should be in another thread.

Best regards, Thank you

2 Answers2

0

If the question is to terminate a outerloop from the innerloop, you can do it using labelled loops in Java.

public static void main(String[] args) {


    int count =0;

    parentLoop:
    while(true) {  // Outer while loop is labelled as parentLoop
        while(true) {
            count++;
            if(count==100) {
                break;
            }
        }
        while(true) {
            count++;
            if(count>150) {
                break parentLoop; // Breaking the parentLoop from innerloop
            }
        }
    }

    System.out.println("Iterated "+count+" times. ");
}
  • Here Label name is parentLoop, a colon ( : ), followed by the while Loop
0

consider this answer: How to break out of nested loops in Java?

You can use labeled loops to break out of nested loops.

class Scratch {
        public static void main( String[] args ) {
            outermark: while(true){//main loop
                while(true){
                    if(Math.random() > 0.6) break outermark; else break;
                }
                System.out.println("outer loop"); // unreachable code
            }
        }
    }

Sample code that can achieve the same using java Thread class.

class Scratch {
    public static void main( String[] args ) {


        Thread thread = new Thread( () -> {
            System.out.println( "thread running" );
            while ( Math.random() < .6 ) {
                System.out.println("thread is trying to break");
                if ( Math.random() > .6 ) {
                    break;
                }
                System.out.println("thread failed to break");
            }

            System.out.println("thread completing");
        } );

        thread.run();
        while ( true ) {
            if ( !thread.isAlive() ) break;
        }

        System.out.println( "finished program" );
    }
}
purple
  • 136
  • 1
  • 7