Skip to content Skip to sidebar Skip to footer

Rerunning A Thread

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()

Solution 1:


Solution 2:

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.


Solution 3:

You can create one infinite thread that will play your sound every time you trigger it to do so, using synchronized, wait and notify commands.

class InfininteTriggerableThread extends Thread {


    private volatile boolean paused = true;
    private final Object pauseObject = new Object();


    @Override
    public void run(){
        try {
            while(true){
                while (paused) {
                    synchronized(pauseObject) {
                        pauseObject.wait();
                    }
                }
                // do your action here
                paused = true;
            }
        } catch (InterruptedException e) { }
    }



    // trigger method
    public void trigger(){
        paused = false;
        synchronized(pauseObject) {
            pauseObject.notify();
        }
    }

};

Solution 4:

As I promised before, I tested thread that uses Looper to run infinitely and can accept Runnables to execute them one by one.

// create a custom Thread class
class ThreadWorking extends Thread {

    public volatile Handler handler;

    @Override
    public void run(){
        // between Looper.prepare() and Looper.loop() we need to create a handler
        // which will receive messages and runnables for this thread
        Looper.prepare();
        handler = new Handler();
        Looper.loop();
    }
};

// then create new thread and start it
final ThreadWorking threadWorking = new ThreadWorking();
threadWorking.start();
Log.println(Log.DEBUG, "thread test", "New thread started");

Now the new Thread is looping and can receive Messages and Runnables by its Handler. They will be stored in internal queue and executed one after another in this particular thread. To test, I send Runnable on button click:

threadWorking.handler.post(new Runnable(){
    @Override
    public void run() {
        try {
             Log.println(Log.DEBUG, "thread test", "Executed: " + System.currentTimeMillis()/1000);
             Thread.sleep(2000);
        } catch (InterruptedException e) { }
    }
});

After I launched an app and clicked the button several times quickly, log shows this:

04-09 16:21:56.599: D/thread test(19264): New thread started
04-09 16:21:59.569: D/thread test(19264): Executed: 1397046119
04-09 16:22:01.569: D/thread test(19264): Executed: 1397046121
04-09 16:22:03.569: D/thread test(19264): Executed: 1397046123
04-09 16:22:05.569: D/thread test(19264): Executed: 1397046125
04-09 16:22:07.569: D/thread test(19264): Executed: 1397046127
04-09 16:22:09.569: D/thread test(19264): Executed: 1397046129

So everything is working correctly as expected, without freezing UI or creating additional threads.


Post a Comment for "Rerunning A Thread"