ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/framework/dispatcher/BaseBlockResponseThread.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.framework.dispatcher; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import com.baidu.duer.dcs.framework.DcsResponseDispatcher; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import com.baidu.duer.dcs.framework.message.DcsResponseBody; |
| TMBOY | 45:2aa9f933c8d2 | 20 | |
| TMBOY | 45:2aa9f933c8d2 | 21 | import java.util.concurrent.BlockingQueue; |
| TMBOY | 45:2aa9f933c8d2 | 22 | |
| TMBOY | 45:2aa9f933c8d2 | 23 | /** |
| TMBOY | 45:2aa9f933c8d2 | 24 | * 单独线程阻塞控制response回调处理 |
| TMBOY | 45:2aa9f933c8d2 | 25 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 26 | * Created by wuruisheng on 2017/6/1. |
| TMBOY | 45:2aa9f933c8d2 | 27 | */ |
| TMBOY | 45:2aa9f933c8d2 | 28 | public abstract class BaseBlockResponseThread extends Thread { |
| TMBOY | 45:2aa9f933c8d2 | 29 | private static final String TAG = BaseBlockResponseThread.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 30 | private BlockingQueue<DcsResponseBody> responseBodyDeque; |
| TMBOY | 45:2aa9f933c8d2 | 31 | private DcsResponseDispatcher.IDcsResponseHandler responseHandler; |
| TMBOY | 45:2aa9f933c8d2 | 32 | private volatile boolean block; |
| TMBOY | 45:2aa9f933c8d2 | 33 | private volatile boolean isStop; |
| TMBOY | 45:2aa9f933c8d2 | 34 | |
| TMBOY | 45:2aa9f933c8d2 | 35 | public BaseBlockResponseThread(BlockingQueue<DcsResponseBody> responseBodyDeque, |
| TMBOY | 45:2aa9f933c8d2 | 36 | DcsResponseDispatcher.IDcsResponseHandler responseHandler, String threadName) { |
| TMBOY | 45:2aa9f933c8d2 | 37 | this.responseBodyDeque = responseBodyDeque; |
| TMBOY | 45:2aa9f933c8d2 | 38 | this.responseHandler = responseHandler; |
| TMBOY | 45:2aa9f933c8d2 | 39 | setName(threadName); |
| TMBOY | 45:2aa9f933c8d2 | 40 | } |
| TMBOY | 45:2aa9f933c8d2 | 41 | |
| TMBOY | 45:2aa9f933c8d2 | 42 | public synchronized void block() { |
| TMBOY | 45:2aa9f933c8d2 | 43 | block = true; |
| TMBOY | 45:2aa9f933c8d2 | 44 | } |
| TMBOY | 45:2aa9f933c8d2 | 45 | |
| TMBOY | 45:2aa9f933c8d2 | 46 | public synchronized void unblock() { |
| TMBOY | 45:2aa9f933c8d2 | 47 | block = false; |
| TMBOY | 45:2aa9f933c8d2 | 48 | notify(); |
| TMBOY | 45:2aa9f933c8d2 | 49 | } |
| TMBOY | 45:2aa9f933c8d2 | 50 | |
| TMBOY | 45:2aa9f933c8d2 | 51 | public synchronized void clear() { |
| TMBOY | 45:2aa9f933c8d2 | 52 | responseBodyDeque.clear(); |
| TMBOY | 45:2aa9f933c8d2 | 53 | } |
| TMBOY | 45:2aa9f933c8d2 | 54 | |
| TMBOY | 45:2aa9f933c8d2 | 55 | public synchronized void stopThread() { |
| TMBOY | 45:2aa9f933c8d2 | 56 | clear(); |
| TMBOY | 45:2aa9f933c8d2 | 57 | isStop = true; |
| TMBOY | 45:2aa9f933c8d2 | 58 | this.interrupt(); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } |
| TMBOY | 45:2aa9f933c8d2 | 60 | |
| TMBOY | 45:2aa9f933c8d2 | 61 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 62 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 63 | while (!isStop) { |
| TMBOY | 45:2aa9f933c8d2 | 64 | try { |
| TMBOY | 45:2aa9f933c8d2 | 65 | synchronized (this) { |
| TMBOY | 45:2aa9f933c8d2 | 66 | if (block) { |
| TMBOY | 45:2aa9f933c8d2 | 67 | wait(); |
| TMBOY | 45:2aa9f933c8d2 | 68 | } |
| TMBOY | 45:2aa9f933c8d2 | 69 | } |
| TMBOY | 45:2aa9f933c8d2 | 70 | |
| TMBOY | 45:2aa9f933c8d2 | 71 | if (responseHandler != null) { |
| TMBOY | 45:2aa9f933c8d2 | 72 | DcsResponseBody responseBody = responseBodyDeque.take(); |
| TMBOY | 45:2aa9f933c8d2 | 73 | responseHandler.onResponse(responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 74 | |
| TMBOY | 45:2aa9f933c8d2 | 75 | if (shouldBlock(responseBody)) { |
| TMBOY | 45:2aa9f933c8d2 | 76 | block = true; |
| TMBOY | 45:2aa9f933c8d2 | 77 | } |
| TMBOY | 45:2aa9f933c8d2 | 78 | } |
| TMBOY | 45:2aa9f933c8d2 | 79 | } catch (InterruptedException e) { |
| TMBOY | 45:2aa9f933c8d2 | 80 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 81 | } |
| TMBOY | 45:2aa9f933c8d2 | 82 | } |
| TMBOY | 45:2aa9f933c8d2 | 83 | } |
| TMBOY | 45:2aa9f933c8d2 | 84 | |
| TMBOY | 45:2aa9f933c8d2 | 85 | abstract boolean shouldBlock(DcsResponseBody responseBody); |
| TMBOY | 45:2aa9f933c8d2 | 86 | } |
