java 异步接口同步调用的方式

  1. 方式一,wait / notify
    示例:WebClient.getInstance().uploadFile方法是一个异步的网络请求接口,上传一个分片文件,
    下面代码就通过wait/notify把这个接口的调用改为同步方法;
private boolean mRequestResult = false;
private final Object mRequestSync = new Object();
boolean requestForUploadFile(final String filePath, final long contentOffset, final long contentLength) {
        Log.d(TAG, "requestForUploadFile::Start:");
        mRequestResult = false;
        try {
            WebClient.getInstance().uploadFile(mContext, mUploadBean, filePath, contentOffset, contentLength, new RequestListener() {
                @Override
                public void onSuccess(Response response) {
                    String contentString = response.getContentString();
                    Log.d(TAG, "requestForUploadFile::onSuccess: contentString=" + contentString);
                    mRequestResult = true;
                    synchronized (mRequestSync) {
                        mRequestSync .notify();
                    }
                }

                @Override
                public void onFailure(int errCode, String errMsg) {
                    Log.d(TAG, "requestForUploadFile::onFailure: errCode=" + errCode +
                            " errMsg=" + errMsg);
                    mRequestResult = false;
                    synchronized (mRequestSync) {
                        mRequestSync.notify();
                    }
                }
            });
            synchronized (mRequestSync) {
                mRequestSync.wait();
            }
        } catch (InterruptedException e) {
            Log.d(TAG, "requestForUploadFile::InterruptedException:");
            e.printStackTrace();
        }

        Log.d(TAG, "requestForUploadFile::End: mRequestResult =" + mRequestResult );
        return mRequestResult ;
    }
  1. 使用 CountDownLatch
private CountDownLatch mRequestLatch;
private boolean mRequestResult = false;
boolean requestForUploadFile(final String filePath, final long contentOffset, final long contentLength) {
        Log.d(TAG, "requestForUploadFile::Start:");
        mRequestLatch = new CountDownLatch(1);
        mRequestResult = false;
        try {
            WebClient.getInstance().uploadFile(mContext, mUploadBean, filePath, contentOffset, contentLength, new RequestListener() {
                @Override
                public void onSuccess(Response response) {
                    String contentString = response.getContentString();
                    Log.d(TAG, "requestForUploadFile::onSuccess: contentString=" + contentString);
                    mRequestResult = true;
                    mRequestLatch.countDown();
                }

                @Override
                public void onFailure(int errCode, String errMsg) {
                    Log.d(TAG, "requestForUploadFile::onFailure: errCode=" + errCode +
                            " errMsg=" + errMsg);
                    mRequestResult = false;
                    mRequestLatch.countDown();
                }
            });
            mRequestLatch.await();
        } catch (InterruptedException e) {
            Log.d(TAG, "requestForUploadFile::InterruptedException:");
            e.printStackTrace();
        }

        Log.d(TAG, "requestForUploadFile::End: mVisionRequestResult=" + mVisionRequestResult);
        return mRequestResult;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值