重新运行一个线程 [英] Rerunning a thread

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

问题描述

在我的Andr​​oid游戏,我产卵一个单独的线程玩的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 execute thread.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屋!

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