在不影响现有代码的情况下中断主线程
Interrupting the main thread with no impact on existing code
如何中断主线程,而不影响现有代码执行?
因为你知道代码 Thread.sleep();对之前的代码有影响。
我特别需要的是一个代码,它可以强制程序脉冲 1 秒,然后 运行 其余部分。但不影响打洞程序(我指的是之前写的代码)
我为我的问题找到了一个解决方案:
我们可以为此使用一组线程和一组处理程序以及 postDelayed 方法。
例如这段代码:
for (int i = 0; i < executeCount; i++) {
final int j = i;
arrayThread.add(new Thread(new Runnable() {
@Override
public void run() {
arrayhandler.get(j).postDelayed(new Runnable() {
@Override
public void run() {
doSomeThings();
if(j == executeCount -1){
doNextCodes();
}
}
}
, (j * WaitingTime));
}
}));
arrayThread.get(j).start();
}
此代码每 "WaitingTime" 秒执行 "doSomeThings();" "executeCount" 次。最后当 "j == executeCount -1" 程序执行 "doNextCodes();" .
如何中断主线程,而不影响现有代码执行?
因为你知道代码 Thread.sleep();对之前的代码有影响。
我特别需要的是一个代码,它可以强制程序脉冲 1 秒,然后 运行 其余部分。但不影响打洞程序(我指的是之前写的代码)
我为我的问题找到了一个解决方案: 我们可以为此使用一组线程和一组处理程序以及 postDelayed 方法。
例如这段代码:
for (int i = 0; i < executeCount; i++) {
final int j = i;
arrayThread.add(new Thread(new Runnable() {
@Override
public void run() {
arrayhandler.get(j).postDelayed(new Runnable() {
@Override
public void run() {
doSomeThings();
if(j == executeCount -1){
doNextCodes();
}
}
}
, (j * WaitingTime));
}
}));
arrayThread.get(j).start();
}
此代码每 "WaitingTime" 秒执行 "doSomeThings();" "executeCount" 次。最后当 "j == executeCount -1" 程序执行 "doNextCodes();" .