博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java--流程控制
阅读量:2350 次
发布时间:2019-05-10

本文共 1023 字,大约阅读时间需要 3 分钟。

retry关键字

看源码的时候看到retry关键字,百度了一下,然后自己写了几个demo。

retry和continue组合

int count = 0;		retry: for (int i=0;;i++) {			for (int j=0;;j++) {				System.out.println("before i:"+i+" j:"+j);				System.out.println("count:" + (count++));				try {					Thread.sleep(100);				} catch (InterruptedException e) {					e.printStackTrace();				}								if (count == 5) {					System.out.println("已经大于5了");					continue retry;				}				System.out.println("after i:"+i+" j:"+j);			}		}

运行结果:

总结:retry和continue组合使用时,执行到continue时将推出当前for循环,执行for循环后的代码。

retry和break语句组合使用。

int count = 0;		retry: for (int i=0;;i++) {			for (int j=0;;j++) {				System.out.println("before i:"+i+" j:"+j);				System.out.println("count:" + (count++));				try {					Thread.sleep(100);				} catch (InterruptedException e) {					e.printStackTrace();				}								if (count == 5) {					System.out.println("已经大于5了");					break retry;				}				System.out.println("after i:"+i+" j:"+j);			}		}

执行结果:

总结:retry和break组合使用,执行到break时将会推出当前for循环和外层for循环,只会退出两层循环,也就是说如果有三层for循环,只会推出到第一层的循环中。

转载地址:http://vuhvb.baihongyu.com/

你可能感兴趣的文章
AccessDeniedException: /opt/elasticsearch-7.0.0/config/elasticsearch.keystore
查看>>
bootstrap-table 父子表 联动表 完整例子
查看>>
Spring Cloud 2.x完整入门Demo样例(Greenwich版本)
查看>>
Spring Cloud 2.x学习笔记:2、feign改进(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:3、Hystrix(Greenwich版本)
查看>>
SpringCloud 2.x学习笔记:4、Zuul(Greenwich版本)
查看>>
ant build.xml教程详解
查看>>
Angle和XBGoost以及Spark的性能对比
查看>>
碎片清理
查看>>
程序员不能错过的技术网站
查看>>
如何使用Git上传和更新项目至Github
查看>>
选择排序(分析+代码调优)
查看>>
Docker
查看>>
代码优化建议,44条代码优化细节
查看>>
快速排序(图解分析+代码调优)
查看>>
Java基础面试总结
查看>>
HashMap遍历几种方式比较(传统的Map迭代方式对比JDK8的迭代方式)
查看>>
Java面试& HashMap实现原理分析
查看>>
PS修改动图字幕
查看>>
八大基础排序总结
查看>>