我正在尝试为我的libgdx游戏创建暂停状态。当暂停按钮被击中时,暂停的变量变为真,我试图让我的倒数计时器线程t3进入睡眠状态几秒钟。但是,当我这样做时,计时器继续倒计时,但我的渲染方法似乎进入睡眠状态。我不明白为什么会这样,请你帮我理解为什么以及如何让我的倒数计时器线程进入睡眠状态。我的计时器类实现了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();
}
}
}
答案 0 :(得分:1)
sleep
是一个静态函数,它在调用它的Thread上运行,在本例中是拥有t3对象而不是t3线程本身的Thread,我认为它是你的渲染线程。这就是你的渲染线程进入睡眠状态的原因。
要将倒计时线程发送到睡眠状态,您需要让倒计时线程调用睡眠本身,例如通过向其发送消息。
答案 1 :(得分:0)
您可以在线程中收听事件。虽然事件未被触发,但线程会休眠。如果你发起事件(从另一个线程),线程立即再次工作。