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,"","");