首先呢, 就算是抄例子,也不能抄错误:)既然线程呢,就一定是实现Runable接口,
而要新建立线程呢,就是new Thread(), 因为Thread类本身就是实现Runable接口的。
Runable()就一个方法run().所以,本程序void run() 就是线程开始运行了。
public class Aninatir extends Applet implements Runnable {
Thread runner;
int x,y;
public void init(){
x=y=0;
} public void start() {
//还是运行现成
runner = new Thread(this);
runner.start();
} public void run() {
while (true) {
x += 4;
y += 4;
repaint();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
;
}
}
} public void paint(Graphics g) {
g.fillOval(x, y, 50, 5);
}
}
而要新建立线程呢,就是new Thread(), 因为Thread类本身就是实现Runable接口的。
Runable()就一个方法run().所以,本程序void run() 就是线程开始运行了。
public class Aninatir extends Applet implements Runnable {
Thread runner;
int x,y;
public void init(){
x=y=0;
} public void start() {
//还是运行现成
runner = new Thread(this);
runner.start();
} public void run() {
while (true) {
x += 4;
y += 4;
repaint();
try {
Thread.sleep(250);
} catch (InterruptedException e) {
;
}
}
} public void paint(Graphics g) {
g.fillOval(x, y, 50, 5);
}
}
|
start()
| |---paint()
run()---------|---sleep(250)
| |---repaint()
stop()