创建线程的方法:
1、继承Thread类重写run()方法
public class testThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
}
}
2、实现Runnable类重写run()方法
public class testThread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
}
}
其实还可以这样写
Runnable testThread = new Runnable(){
public void run(){
}
}
开启线程:
一般开启线程是直接
1、new testThread().start(); 或 2、new Thread(testThread).start(); 或 3、GameView.this.post(testThread);
这样来开启线程。
假如我们游戏中要做到延时开启线程就应该
GameView.this.postDelayed(testThread, 1000); //第一个参数为线程名,第二个是时间以毫秒为单位
停止线程则应该
GameView.this.getHandler().removeCallbacks(testThread); //参数为线程名