Java使用另一个线程检查线程的状态(Java checking the status of threads by using another thread)
我有一个程序,其中一堆线程执行一些任务。 我想知道所有这些线程何时完成,以便我可以用其结果做其他一些事情。 我不想使用像
Thread.join
这样的东西。 我更喜欢的是另一个线程检查所有这些线程,当它看到它们完成时,它执行一些特殊任务。 关于如何做到这一点的任何想法?I have a program where a bunch of threads carry out some task. I want to know when all these threads are finished so that I can do some other stuff with their results. I don't want to use things like
Thread.join
. What I prefer is another thread checking on all these threads and when it sees that they are finished, it carries out some special task. Any ideas about how this can be done ?
原文:https://stackoverflow.com/questions/13000446
满意答案
你这样开始:
int value = 123; bgw1.RunWorkerAsync(value); // argument: value, the int will be boxed
接着
private void worker_DoWork(object sender, DoWorkEventArgs e) { int value = (int) e.Argument; // the 'argument' parameter resurfaces here ... // and to transport a result back to the main thread double result = 0.1 * value; e.Result = result; } // the Completed handler should follow this pattern // for Error and (optionally) Cancellation handling private void worker_Completed(object sender, RunWorkerCompletedEventArgs e) { // check error, check cancel, then use result if (e.Error != null) { // handle the error } else if (e.Cancelled) { // handle cancellation } else { double result = (double) e.Result; // use it on the UI thread } // general cleanup code, runs when there was an error or not. }
You start it like this:
int value = 123; bgw1.RunWorkerAsync(argument: value); // the int will be boxed
and then
private void worker_DoWork(object sender, DoWorkEventArgs e) { int value = (int) e.Argument; // the 'argument' parameter resurfaces here ... // and to transport a result back to the main thread double result = 0.1 * value; e.Result = result; } // the Completed handler should follow this pattern // for Error and (optionally) Cancellation handling private void worker_Completed(object sender, RunWorkerCompletedEventArgs e) { // check error, check cancel, then use result if (e.Error != null) { // handle the error } else if (e.Cancelled) { // handle cancellation } else { double result = (double) e.Result; // use it on the UI thread } // general cleanup code, runs when there was an error or not. }
相关问答
更多后台工作者异常处理(Background worker exception handling)
向后台工作者发送参数?(Sending Arguments To Background Worker?)
后台工作者worker_spi示例PostgreSQL(Background Worker worker_spi Example PostgreSQL)
WPF后台工作者和ProgressBar(WPF Background Worker and ProgressBar)
向后台工作者传递值(Passing Values To and From Background Worker)
背景工作者(Background worker)
线程和后台工作者(Threading and Background Worker)
用Func和Action创建一个后台工作者类(Creating a background worker class with Func and Action)
来自后台工作者的文本框文本?(Textbox text from background worker?)
从不同的类访问后台工作者(Access background worker from a different class)
相关文章
更多java线程状态详解(6种)
Storm-源码分析-Topology Submit-Executor-mk-threads
线程报错 thread
关于Thread类中的start()方法和run()方法
http status 汇总
一步一步掌握java的线程机制(二)----Thread的生命周期
Java 多线程编程
引入thread后socket接受不了报文了
org.apache.zookeeper.server.NIOServerCnxn - Thread Thread[main,5,main] died java.lang.NoSuchFieldError: AuthFailed
一步一步掌握java的线程机制(一)----创建线程
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!