質問

I am currently trying to create a pause state for my libgdx game. When the pause button is hit the paused variable becomes true and I am trying to get my countdown timer thread called t3 to sleep for a few seconds. However when I do this the timer continues to countdown but my render method appears to go to sleep instead. I don't understand why this is happening can you please help me understand why and how I can get my countdown timer thread to sleep. My timer class implements runnable.

tim = new timer();  //on create start the countdown timer thread
t3 = new Thread(tim);//name of countdown timer thread
t3.start();//start the thread

if (paused == true) {
    System.out.println(paused);
    try {
        t3.sleep(5000);           //put the countdown timer thread to sleep for 5s
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
paused = false;



//this is the run method for the timer class aka the countdown
public void run() { 

    while (timekeeper.TimerRunning == true) {
        System.out.println(Timekeeper.gettimer());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (Timekeeper.gettimer().get() == 0) {

            timekeeper.TimerRunning = false;

        } else {

            Timekeeper.gettimer().decrementAndGet();

        }

    }

}
役に立ちましたか?

解決

sleep is a static function and operates on the Thread which has invoked it, in this case the Thread which owns t3 object and not the t3 thread itself, which I assume it's your rendering thread. That's why your rendering Thread goes to sleep.

To send the countdown thread to sleep, you need to ask countdown thread to call sleep itself, for example by sending a message to it.

他のヒント

You can listen on an event in your thread. While the event is not fired the thread sleeps. And if you fire the event (from another thread) the thread ist working again immediately.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top