1、点击某个按钮或发生某个信息指令,进而执行一个任务(如请求接口)
2、但是!由于快速点击,或发生信息指令时、短时间内频繁执行了多次
3、当我们收到某个信息指令时,只需执行一次任务时,代码逻辑显得更加杂乱
因此,本文加入了方法体任务队列概念,不多说,直接上源码!!
/**
* 方法体任务队列
* 注:
* offer() :如果BlockingQueue可以容纳,则返回true,否则返回false.(本方法不阻塞当前执行方法的线程)
* poll() :若队列为空,返回null(本方法不阻塞当前执行方法的线程)
* peek() :取出队头的元素,并不删除
*
* 1、produceChain与consumeChain需要成对出现
* 2、produceSingle与consumeSingle需要成对出现
*
*/
class FunQueueTask {
//链式任务队列
private var chainBasket: BlockingQueue<() -> T?> = LinkedBlockingQueue()
//链式任务队列标记位
private var flag = true
//单任务队列
private var singleBasket: BlockingQueue<() -> T?> = LinkedBlockingQueue(1)
//******************************链式队列*************************************************
fun produceChain(block: () -> T?): Boolean {
val isSucceed = chainBasket.offer(block)
YLogUtils.d("链式任务--生产", block, "成功?", isSucceed)
//如果队列只有一个元素,则立马出列
if (flag && isSucceed) {
flag = false
val t = chainBasket.poll()
YLogUtils.d("链式任务--队列只有一个元素,立马消费", t)
t?.invoke()
}
return isSucceed
}
fun consumeChain() {
val t = chainBasket.poll()
YLogUtils.d("链式任务--消费", t)
t?.invoke()
//若方法体队列已全部出列,则标记为true
if (t == null) {
flag = true
}
}
//*******************************单任务队列*************************************************
fun produceSingle(block: () -> T?): Boolean {
val isSucceed = singleBasket.offer(block)
YLogUtils.d("单任务--生产", block, "成功?", isSucceed)
//如果队列有元素,则立马取出,但不消费
if (isSucceed) {
val t = singleBasket.peek()
YLogUtils.d("单任务--队列有元素,取出元素,但不消费", t)
t?.invoke()
}
return isSucceed
}
fun consumeSingle() {
val t = singleBasket.poll()
YLogUtils.d("单任务-消费", t)
}
}
/**
* 任务经营者(单例,全局任务)
*/
class FunTaskManager private constructor() {
//方法体任务队列
private val funQueueTask = FunQueueTask()
fun getFunQueueTask(): FunQueueTask {
return funQueueTask
}
private object Holder {
val instance = FunTaskManager()
}
companion object {
@Synchronized
fun getInstance(): FunTaskManager {
return Holder.instance
}
}
}
/**
* @Author Administrator
* @Date 2020/3/12-11:38
* @TODO mvc 任务队列请求
*/
class QueueApiSimpleActivity : BaseActivity() {
companion object {
fun startAct(context: Context) {
ActivityUtils.startActivity(context, QueueApiSimpleActivity::class.java)
}
}
private val simpleMode = ApiSimpleModel()
override fun initLayoutID(): Int = R.layout.simple_api_test_activity_queue
override fun initUI(savedInstanceState: Bundle?) {
}
fun onRequest(view: View) {
when (view.id) {
R.id.requestData_common -> {
for (i in 1..10) {
sendGiftCommon(SendGiftVo("123", "456", 1, 100, 0))
}
}
R.id.requestData_fun_single -> {
for (i in 1..10) {
FunTaskManager.getInstance().getFunQueueTask().produceSingle {
sendGiftFunSingle(SendGiftVo("123", "456", 1, 100, 0))
}
}
}
R.id.requestData_fun_chain -> {
for (i in 1..10) {
FunTaskManager.getInstance().getFunQueueTask().produceChain {
sendGiftFunChain(SendGiftVo("123", "456", 1, 100, 0))
}
}
}
}
}
/**
* 普通请求
*/
private fun sendGiftCommon(sendGiftVo: SendGiftVo) {
simpleMode.sendGift(RxObserver(doNext = { it ->
tv_msg.setText(it.msg)
}, doError = { _, _ ->
}), lifecycleProvider, sendGiftVo)
}
/**
* 单任务队列
*/
private fun sendGiftFunSingle(sendGiftVo: SendGiftVo) {
simpleMode.sendGift(RxObserver(doNext = { it ->
tv_msg.setText(it.msg)
FunTaskManager.getInstance().getFunQueueTask().consumeSingle()
}, doError = { _, _ ->
FunTaskManager.getInstance().getFunQueueTask().consumeSingle()
}), lifecycleProvider, sendGiftVo)
}
/**
* 链式队列
*/
private fun sendGiftFunChain(sendGiftVo: SendGiftVo) {
simpleMode.sendGift(RxObserver(doNext = { it ->
tv_msg.setText(it.msg)
FunTaskManager.getInstance().getFunQueueTask().consumeChain()
}, doError = { _, _ ->
FunTaskManager.getInstance().getFunQueueTask().consumeChain()
}), lifecycleProvider, sendGiftVo)
}
}
5.1、普通请求结果
5.2.单任务队列
5.3、链式队列
github地址 YLibrary