/// <summary> /// 多线程并行建图 /// </summary> /// <param name="threadCount">并行线程数量</param> /// <param name="log">日志处理</param> private void buildGraph(int threadCount, AutoCSer.Log.ILog log) { LeftArray <Node> reader = new LeftArray <Node>(Boot.Nodes.Values.getArray()); int taskCount = threadCount - 1; bool isError = false; AutoCSer.Threading.AutoWaitCount waitCount = new AutoCSer.Threading.AutoWaitCount(taskCount); ThreadBuilder[] builders = new ThreadBuilder[threadCount]; try { for (int builderIndex = 0; builderIndex != builders.Length; builders[builderIndex++] = new ThreadBuilder(Boot, waitCount)) { ; } do { Node[] readerArray = reader.Array; int count = reader.Length / threadCount, index = 0; for (int builderIndex = 0; builderIndex != taskCount; ++builderIndex) { builders[builderIndex].SetThread(readerArray, index, count); index += count; } builders[taskCount].Set(readerArray, index, reader.Length); builders[taskCount].Build(); waitCount.WaitSet(taskCount); reader.Length = 0; foreach (ThreadBuilder builder in builders) { if (builder.ThreadException == null) { reader.Add(ref builder.Writer); } else { log.add(Log.LogType.Error, builder.ThreadException); isError = true; } } }while (reader.Length != 0 && !isError); } finally { foreach (ThreadBuilder builder in builders) { if (builder != null && builder.ThreadException == null) { builder.FreeThread(); } } } }
/// <summary> /// Trie 图创建器 /// </summary> /// <param name="boot">根节点</param> /// <param name="waitCount">建图线程任务完成计数</param> public ThreadBuilder(Node boot, AutoCSer.Threading.AutoWaitCount waitCount) : base(boot) { this.waitCount = waitCount; threadWait.Set(0); AutoCSer.Threading.ThreadPool.TinyBackground.FastStart(build); }