您的位置:首页 > 其它

对于线程的一些理解

2009-02-05 09:00 337 查看
#isubb#package untitled1;

import javax.microedition.lcdui.*;

public class tetriscanvasdemo extends canvas implements runnable{
int drawx,drawy;
image source;
image copy;

tetriscanvasdemo()
{
drawx=this.getwidth()/2;
drawy=0;
copy=image.createimage(this.getwidth(),this.getheight());
}
protected void paint(graphics g)
{
graphics offscreen=copy.getgraphics();
offscreen.setcolor(255,255,255);
offscreen.fillrect(0,0,this.getwidth(),this.getheight());
offscreen.setcolor(0,0,0);
offscreen.fillrect(drawx,drawy,10,10);
g.drawimage(copy,0,0,graphics.left |graphics.top);
}
public void run()
{
while(true)
{
this.down();
repaint();
try
{
thread.sleep(50);;

}
catch(interruptedexception e)
{
system.out.println("sleep error:"+e.getmessage());
}
}
}
public void start()
{
thread t=new thread(this);
t.start();
}
protected void down()
{
if(drawy {
drawy=drawy+1;
}
else
{
drawy=0;
}
}
}

在这个类中,如果没有implemnt runable那就是一般的canvas界面,执行完构造函数再paint函数显示就完了。加了runable,在start里,[color=#ff0000]thread t=new thread(this)就将此canvas定义为一个线程了,[/color]t.start()启动线程,在run里就可以写这个线程中做些什么了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: