• semaphore demo 并行 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


    import 'dart:async';
    import 'package:semaphore/semaphore.dart';
    import 'dart:io';
    import 'dart:convert';
    import 'dart:math';
    
    import "dart:math";
    
    void main() async {
      int maxCount = 5;
      LocalSemaphore sm = LocalSemaphore(maxCount);
      List<Future> tasks = [];
      int end = 0;
      for (int i = 0; i < maxCount; i++) {
        tasks.add(eventLoop(sm, '$i', end));
      }
      await Future.wait(tasks);
    }
    
    Future eventLoop(LocalSemaphore sm, String key, int end) async {
      await sm.acquire();
      await doWork(key, 0, true, end);
      sm.release();
    }
    
    Future doWork(String key, int i, bool ok, int end) async {
      print('getting in thread: $key, bool is: $ok');
      bool b = ok;
      int a = end;
      if (b) {
        print('thread: $key, current: $i');
        await Future.delayed(Duration(seconds: 1));
        a += 1;
          if (a > 10) {
            print('change the bool.here thread: $key, and a is: $a');
            b = false;
          }else{
            b = true;
          }
          print('a is: $a');
        await doWork(key, i + 1, b, a);
      }
    }
    

      

  • 相关阅读:
    货币转换函数:CURRENCY_CONVERTING_FACTOR
    Function程序设计及应用
    工具栏对象GUI Status 与GUI Title
    Message的定义類型
    Report List 报表开发
    SpringMVC构建Restful。
    Redis主从复制
    转:Redis配置文件详解
    Redis入门学习
    Redis开启远程登录连接。
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10799106.html
Copyright © 2020-2023  润新知