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