java 一个任务需要多个线程按照一定顺序执行,如果执行错误就执行错误函数
//在一定时间内,等待某个事件//小于currentStatus的标志位都是true,才能继续向下,不然就报错 // status 状态表,currentStatus 当前线程状态位置,timeout 超时时间,funcSuccess 规定时间内要执行的函数,funcError 超过规定时间后要执行的函数 void delayRestartBluetooth(boolean[] status, int currentStatus, int timeout,Consumer<String> funcSuccess,Consumer<String> funcError,String funcSuccessStr,String funcErrorStr){ //延时执行 Thread workerThead = new Thread() { @RequiresApi(api = Build.VERSION_CODES.N) public void run() { try { //检测,之前的任务是否完成//未完成就一直等待 boolean isEnable = false; if(currentStatus==0)isEnable = true; while (isEnable == false){ for(int i=0;i<currentStatus;i++){ if(status[i]==false){isEnable = false;break;} else {isEnable = true;} } } if(isEnable == true){ funcSuccess.accept(funcSuccessStr); status[currentStatus] = true; } } catch (Exception e) { status[currentStatus] = false; e.printStackTrace(); } } }; workerThead.start(); Thread checkThread = new Thread() { @RequiresApi(api = Build.VERSION_CODES.N) public void run() { boolean isEnable = false; int count=10; while (isEnable == false && count<timeout){ try { sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } count += 50; for(int i=0;i<currentStatus+1;i++){ if(status[i]==false){isEnable = false;break;} else {isEnable = true;} } } if(isEnable == false){ try { funcError.accept(funcErrorStr); } catch (Exception e) { e.printStackTrace(); } workerThead.interrupt(); } } }; checkThread.start(); // new Handler().postDelayed(()->{ Log.d(TAG, "ceshi,zhe个只会执行1次: "); },timeout);
改进方案,每一步只需要一个线程就可以
//在一定时间内,等待某个事件//小于currentStatus的标志位都是true,才能继续向下,不然就报错 // status 状态表,currentStatus 当前线程状态位置,timeout 超时时间,funcSuccess 规定时间内要执行的函数,funcError 超过规定时间后要执行的函数 void delayRestartBluetooth(boolean[] status, int currentStatus, int timeout,Consumer<String> funcSuccess,Consumer<String> funcError,String funcSuccessStr,String funcErrorStr){ //延时执行 new Thread() { @RequiresApi(api = Build.VERSION_CODES.N) public void run() { try { //检测,之前的任务是否完成//未完成就一直等待,直到超时 boolean isEnable = false; if(currentStatus==0)isEnable = true; int time=10; int timeStep = 50;//时间步长 while (isEnable == false && time<timeout){ try { sleep(timeStep); } catch (InterruptedException e) { e.printStackTrace(); } time += timeStep; for(int i=0;i<currentStatus;i++){ if(status[i]==false){isEnable = false;break;} else {isEnable = true;} } } if(isEnable == true){ funcSuccess.accept(funcSuccessStr); status[currentStatus] = true; } else { funcError.accept(funcErrorStr); status[currentStatus] = false; } } catch (Exception e) { status[currentStatus] = false; e.printStackTrace(); } } }.start(); // new Handler().postDelayed(()->{ Log.d(TAG, "ceshi,zhe个只会执行1次: "); },timeout); }
具体的使用案例如下
boolean[] status = {false}; Consumer<String> funcError = x ->{}; Consumer<String> funcSuccess = x -> { mBluetoothAdapter.enable(); while (!mBluetoothAdapter.isEnabled()){ try { sleep(10); } catch (InterruptedException e) { } } try { sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } Intent intent = new Intent("com.TransIP2BLE.broadcast.BLEBlueTooth.message"); intent.putExtra("msg","bleRestartOK"); intent.putExtra("message","cmd:upBleRestart,ble restart successful"); localBroadcastManager.sendBroadcast(intent); }; delayRestartBluetooth(status,0,10000,funcSuccess,funcError,"","");
boolean[] status = {false,false,false}; Consumer<String> funcError = x ->{}; Consumer<String> funcSuccess1 = x -> { mBluetoothAdapter.disable(); Log.d(TAG, "restartBluetooth: "); while (mBluetoothAdapter.isEnabled()){ try { sleep(10); } catch (InterruptedException e) { } } try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }; Consumer<String> funcSuccess2 = x -> { mBluetoothAdapter.enable(); while (!mBluetoothAdapter.isEnabled()){ try { sleep(10); } catch (InterruptedException e) { } } Log.d(TAG, "restartBluetooth: "); try { sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } }; Consumer<String> funcSuccess3 = x -> { while (!mBluetoothAdapter.isEnabled()){ try { sleep(10); } catch (InterruptedException e) { } } Intent intent = new Intent("com.TransIP2BLE.broadcast.BLEBlueTooth.message"); intent.putExtra("msg","bleRestartOK"); intent.putExtra("message","cmd:upBleRestart,ble restart successful"); localBroadcastManager.sendBroadcast(intent); Log.d(TAG, "restartBluetooth: "); }; delayRestartBluetooth(status,0,10000,funcSuccess1,funcError,"",""); delayRestartBluetooth(status,1,10000,funcSuccess2,funcError,"",""); delayRestartBluetooth(status,2,10000,funcSuccess3,funcError,"","");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构