JavaDiamonds(俄罗斯方块) 中可暂停线程的实现(1)
下载地址http://www.shengzhen.com.cn/Javadiamonds/游戏中的所有的正在下落的形状都从该类继承package Javablock.model;
public class SuspensibleThread extends Thread { private volatile boolean runing = true; private volatile boolean pause = false; public SuspensibleThread() { }
//run方法不可再被继承了 final public void run() { try { while (runing) { //当线程被暂停时 将会进入runing的循环检测中, //这时有必要使用yield给于其他线程调用continueThread的机会 this.yield(); //如果没有sleep(10)此while循环会占用大量cpu时间 Thread.sleep(10); while (!pause && runing) { runTask();//注意runtask要实现线程安全 } if (!pause && runing) { //synchronized (this)是必须的