吐槽Okhttp的一个语法点

13 篇文章 0 订阅

最近在看okhttp源码的时候,有个语法点我想吐槽一下,先看代码,代码在RealCall.java中

    void executeOn(ExecutorService executorService) {
 1   assert (!Thread.holdsLock(client.dispatcher()));
      boolean success = false;
      try {
        executorService.execute(this);
        success = true;
      } catch (RejectedExecutionException e) {
        InterruptedIOException ioException = new InterruptedIOException("executor rejected");
        ioException.initCause(e);
        transmitter.noMoreExchanges(ioException);
        responseCallback.onFailure(RealCall.this, ioException);
      } finally {
        if (!success) {
          client.dispatcher().finished(this); // This call is no longer running!
        }
      }
    }

1 处用了assert 这个关键字,工作这么些年,很少使用这个关键字,查了下这个关键字的资料,assert 在java 虚拟机中默认不开启,如果要开启那么添加虚拟机参数-ea,如图:
在这里插入图片描述
查找诸多资料后,一致认为这个关键字最多在测试环境或者调试程序的时候,基本不用做业务流程。所以纵然okhttp是非常优秀的框架,基本上开发App用到的网络请求框架都是它,但这里用assert确实不合适。有可能android 虚拟机没开启过这个开关。其实这里应该是throws exception更加合理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值