获取线程的输出 [英] Getting the output of a Thread

查看:139
本文介绍了获取线程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您认为获得线程工作结果的最佳方法是什么?想象一下一个线程执行一些计算,您如何警告主程序计算完成?

What do you think is the best way for obtaining the results of the work of a thread? Imagine a Thread wich does some calculations, how do you warn the main program the calculations are done?

您可以每隔X毫秒轮询一个称为作业完成"的公共变量或其他方式,但是随后您将收到结果,而不是等到它们可用时……主代码将浪费时间等待为他们.另一方面,如果您使用较低的X,则轮询将浪费CPU太多次.

You could poll every X milliseconds for some public variable called "job finished" or something by the way, but then you'll receive the results later than when they would be avaliable... the main code would be losing time waiting for them. On the other hand, if you use a lower X, the CPU would be wasted polling so many times.

那么,您要怎么做才能知道线程或某些线程已经完成了工作?

So, what do you do to be aware that the Thread, or some Threads, have finished their work?

很抱歉,如果它看起来与此其他问题,我想这可能就是 eben 答案的原因.我的意思是运行大量线程,并且知道所有线程何时完成,而无需轮询它们.

Sorry if it looks similar to this other question, that's probably the reason for the eben answer, I suppose. What I meaned was running lots of threads and know when all of them have finished, without polling them.

在考虑使用一批线程在多个CPU之间共享CPU负载时,我进行了更多思考,并知道一批何时完成.我想可以用 Future 对象完成此操作,但是阻塞 get 方法看起来很像一个隐藏的锁,而不是我喜欢的东西.

I was thinking more in the line of sharing the CPU load between multiple CPU's using batches of Threads, and know when a batch has finished. I suppose it can be done with Futures objects, but that blocking get method looks a lot like a hidden lock, not something I like.

感谢大家的支持.尽管我也喜欢 erickson 的答案,但我认为 saua 是最完整的答案,会用我自己的代码.

Thanks everybody for your support. Althought I also liked the answer by erickson, I think saua's the most complete, and the one I'll use in my own code.

推荐答案

除非绝对需要强大的功能和灵活性,否则请不要使用诸如线程之类的低级构造.

Don't use low-level constructs such as threads, unless you absolutely need the power and flexibility.

您可以使用 ExecutorService 例如 ThreadPoolExecutor 提交() Callables .这将返回 Future 对象.

You can use a ExecutorService such as the ThreadPoolExecutor to submit() Callables. This will return a Future object.

使用该Future对象,您可以轻松地检查它是否已完成并获得结果(如果尚未完成,则包括一个阻塞的get()).

Using that Future object you can easily check if it's done and get the result (including a blocking get() if it's not yet done).

那些构造将大大简化最常见的线程操作.

Those constructs will greatly simplify the most common threaded operations.

我想澄清有关阻止get()的信息:

I'd like to clarify about the blocking get():

这个想法是,您想运行一些不需要执行结果的任务(Callable)(计算,资源访问等),而现在不需要结果 .您只需依赖Executor即可在需要时运行代码(如果它是ThreadPoolExecutor,则它将在有可用线程时运行).然后在某个时间点,您可能需要计算结果继续.此时,您应该调用get().如果此时已经执行了任务,则get()将立即返回该值.如果任务未完成,则get()调用将等待,直到任务完成.通常是需要这样做的,因为无论如何都无法完成任务.

The idea is that you want to run some tasks (the Callables) that do some work (calculation, resource access, ...) where you don't need the result right now. You can just depend on the Executor to run your code whenever it wants (if it's a ThreadPoolExecutor then it will run whenever a free Thread is available). Then at some point in time you probably need the result of the calculation to continue. At this point you're supposed to call get(). If the task already ran at that point, then get() will just return the value immediately. If the task didn't complete, then the get() call will wait until the task is completed. This is usually desired since you can't continue without the tasks result anyway.

当您不需要继续该值,但想知道它是否已经可用时(可以在UI中显示某些内容),那么您可以轻松地调用isDone(),并且仅在get()时调用返回true).

When you don't need the value to continue, but would like to know about it if it's already available (possibly to show something in the UI), then you can easily call isDone() and only call get() if that returns true).

这篇关于获取线程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆