Java线程池写法改写为C#线程池写法

下面这段C#代码中的线程池部分还是Java的写法,请大神门帮忙改成C#写法。
示例代码,最终代码啥的都很欢迎!

 /// <summary> /// 用户初始化ak和sk,sdk自己生成token </summary> /// <param name="bucketName"> </param> /// <param name="fileKey"> </param> /// <param name="randomFile"> </param> /// <param name="putPolicy"> </param> /// <param name="putExtra"> </param> /// <param name="jsonObjectRet"> </param> public virtual void execUploadstring bucketName, string fileKey, string randomFile, PutPolicy putPolicy, PutExtra putExtra, JSONObjectRet jsonObjectRet { FileInfo randomFileInfo = null; ExecutorService pool = null; HttpClient mkfileHttpClient = null; int retry = 0; while retry < 3 { string uploadBatch = "SDK-" + Guid.NewGuid;// UUID.randomUUID; retry++; try { if BaseBlockUtil.BLOCK_SIZE < 4 * BaseBlockUtil.MB || BaseBlockUtil.BLOCK_SIZE % 4 * BaseBlockUtil.MB != 0 { jsonObjectRet.onFailurenew Exception"块大小应该为4M的整数倍!"; return; } randomFileInfo = new FileInforandomFile; pool = Executors.newFixedThreadPoolBaseBlockUtil.THREAD_NUN; CompletionService<BlockObject> completionService = new ExecutorCompletionService<BlockObject>pool; if putExtra.processes == null || putExtra.totalSize != randomFileInfo.Length { initPutExtrabucketName, fileKey, randomFile, putExtra, jsonObjectRet; } putExtra.totalSize = randomFileInfo.Length; putPolicy.deadline = Convert.ToStringDateUtil.ToJavaMillisecondsDateUtil.nextNHours1, DateTime.Now; DateTime dealEndDate = DateUtil.nextNMinutes5, DateTime.Now; string token = TokenUtil.getUploadTokenputPolicy; int runnerThread = 0; foreach BlockObject blockObject in putExtra.processes { runnerThread++; while runnerThread > BaseBlockUtil.THREAD_NUN + 1 { completionService.take.get; runnerThread--; } if dealEndDate < DateTime.Now { dealEndDate = DateUtil.nextNMinutes5, DateTime.Now; putPolicy.deadline = Convert.ToStringDateUtil.ToJavaMillisecondsDateUtil.nextNHours1, DateTime.Now; token = TokenUtil.getUploadTokenputPolicy; } //RandomAccessFile blockFile = new RandomAccessFilerandomFile, "r"; blockObject.setCommonParamrandomFile, bucketName, fileKey; HttpClient httpClient = new HttpClient; BlockUpload task = new BlockUploadblockObject, jsonObjectRet, putExtra, token, httpClient, uploadBatch; completionService.submittask; } for int i = 0; i < runnerThread; i++ { completionService.take.get; } pool.shutdown; putPolicy.deadline = Convert.ToStringDateUtil.ToJavaMillisecondsDateUtil.nextNHours1, DateTime.Now; token = TokenUtil.getUploadTokenputPolicy; mkfileHttpClient = WebRequestMethods.Http.HttpClient; BaseBlockUtil util = new BaseBlockUtilnull, jsonObjectRet, putExtra, token, mkfileHttpClient; SliceUploadHttpResult result = util.mkfiletoken, fileKey, putExtra, 0, uploadBatch; if result.Status == 200 { string hash = WetagUtil.getEtagHashrandomFile; if result.Hash.Equalshash { jsonObjectRet.onSuccessresult.toJSON; break; } else { Console.WriteLinefileKey + " 文件hash与服务器不一致,文件重新上传"; putPolicy.overwrite = 1; putExtra.processes = null; } } else { if result.Status == 412 { Console.WriteLinefileKey + " 此文件块有缺失,文件重新上传"; putPolicy.overwrite = 1; putExtra.processes = null; } else { jsonObjectRet.onFailurenew HttpClientExceptionresult.Status, result.Response; break; } } } catch Exception e { jsonObjectRet.onFailuree; } finally { if pool != null { pool.shutdownNow; } if mkfileHttpClient != null { try { mkfileHttpClient.close; } catch IOException { } } if randomFileInfo != null { try { int reTime = 0; while !pool.Terminated && reTime < 50 { reTime++; Thread.Sleep1000; } } catch Exception { Console.WriteLine"file close error"; } } } } }

C# 的 ThreadPool 使用起来比较简单,可以参考一下文档,主要是通过 QueueUserWorkItemWaitCallback 方法来启动一个任务(线程任务),其中 WaitCallback 是一个委托,可以使用 Lambda 表达式来写。

另外也可以用 System.Threading.Tasks.Task 来实现线程池的操作,相关的资料在网上很容易找到。

总的来说 .NET 的线程池不需要过多的去处理线程池本身的事情,只需要关注业务就好。关于线程池的大小,可以用 ThreadPool.SetMaxThreadsThreadPool.SetMinThreads 来设置,但是一般不需要去处理。

发表评论

电子邮件地址不会被公开。 必填项已用*标注