本文介绍了主题:一旦完成如何重新启动线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个方法无效的DoWork(对象输入)这需要大约5秒钟完成。我已阅读,发更适合比线程池对这些较长的操作,但我也遇到了问题。 我点击一个按钮,它调用 threadRun.Start(输入)它运行并完成罚款。我再次点击按钮,收到以下异常: 线程正在运行或终止;它不能重新启动。 你能不能再利用主题?我应该使用线程池?为什么线程更适合长期作战相比,线程池?如果你不能重复使用一个线程,为什么要使用它在所有(即什么好处它提供的)? 解决方案 You can. But you have to code the thread not to terminate but to instead wait for more work. That's what a thread pool does.If you want to re-use a thread, yes.Imagine a thread pool that is serving a large number of quick operations. You don't want to have too many threads, because the computer can only do so many things at a time. Each long operation you make the thread pool do ties up a thread from the pool. So the pool either has to have lots of extra threads or may run short of threads. Neither leads to an efficient thread pool design.For longer operations, the overhead of creating and destroying a thread is very small in comparison to the cost of the operation. So the normal downside of using a thread just for the operation doesn't apply.I'm assuming you mean using a thread dedicated to a job that then terminates over using a thread pool. The advantage is that the number of threads will always equal the number of jobs this way. This means you have to create a thread every time you start a job and destroy a thread every time you finish one, but you never have extra threads nor do you ever run short on threads. (This can be a good thing with I/O bound threads but can be a bad thing if most threads are CPU bound most of the time.) 这篇关于主题:一旦完成如何重新启动线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 08:23