下载地址
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)是必须的
synchronized (this) {
this.wait();
}
}
if (!runing) {
break;
}
}
}
catch (InterruptedException ex) {
runing = false;
}
}
/**在继承runTask方法中应该有sleep方法
* */
public void runTask() throws InterruptedException {
Thread.sleep(1);
}
//暂停
public synchronized void pauseThread() {
pause = true;
}
//继续
public synchronized void continueThread() {
pause = false;
notify();
}
//中止线程
public synchronized void endThread() {
pause = true;
runing = false;
interrupt();
}
//重启线程
public synchronized void restart() {
runing = true;
pause = false;
this.start();
}
public boolean isThreadPause() {
return pause;
}
public boolean isThreadRunnig() {
return runing;
}
}