重新运行一个线程 [英] Rerunning a thread
问题描述
在我的Android游戏,我产卵一个单独的线程玩的Soundpool
类的声音。我每次都要打声我要创建一个新的线程,并执行 thread.start()
。是否有可能一次创建线程和重用?我想这样做,这样我可以避开产卵很多线程的开销。
是否有可能一次创建线程和重用?
块引用>它是不可能的。
可能的解决方案是使用固定大小的线程池(取决于您的要求),并继续提交您的可运行任务的线程池重用了。
确定线程池的完美的大小是很重要的,过少都会有类似的问题,过高会产生性能问题。所以,你需要做一些基准来决定一样。更好的方法是让配置和监控。
您可能想看看的ExecutorService 一> 执行人在Java中了解线程池。
In my android game, I am spawning a separate thread to play a sound of
soundpool
class. Every time I have to play the sound I have to create a new thread and executethread.start()
. Is it possible to create thread once and reuse ? I want to do this so that I can avoid the overhead of spawning a lot of threads.解决方案Is it possible to create thread once and reuse ?
Its not possible.
Possible solution is to use thread pool of fixed size (depends on your requirement) and keep on reusing it by submitting your runnable tasks to the thread pool.
Determining the perfect size of thread pool is important, too less will have the similar problem and too high will have performance issues. So you need to do some benchmarking to determine the same. A better idea is to make it configurable and monitor it.
You may want to look ExecutorService, Executors in java to learn about Thread pools.
这篇关于重新运行一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!