agilord/executor

when run sample code but don't true?

Closed this issue · 5 comments

void func(String msg){
//sleep(const Duration(seconds:1));
print(msg);
}

Executor executor = new Executor(concurrency: 5);
for (int i = 0; i < 20; i++) {
executor.scheduleTask(() async {
// await longDatabaseTask()
// await anotherProcessing()
await func(i.toString());

  print('concurrency :${executor.concurrency}'); //don't true
  print('runningCount :${executor.runningCount}'); //don't true
  print('scheduledCount :${executor.scheduledCount}'); //don't true
  print('waitingCount :${executor.waitingCount}');  //don't true
  print('------------');

});
print("main thread");

}

and how to check all task that have finished

isoos commented

AFAIK sleep is a synchronous call, and suspends the Dart VM, the async tasks are not running with it.

If you want to wait for all the tasks to complete, you can call this one:
https://pub.dev/documentation/executor/latest/executor/Executor/join.html

await executor.join(); will wait for the currently running ones, withWaiting: true will cause to wait for all the currently scheduled ones.

thanks

@mithang not sure if this is still relevant but await Future.delayed(const Duration(seconds: 1)); is the async-friendly version of sleep.

isoos commented

@venkatd that's correct.