在JAVA中创建线程一般使用两种方法
案例继承Thread
class MyRun extends Thread
{
@Override
public void run() {
for (int i=1;i<=100;i++)
{
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void btn1(View view)
{
MyRun myRun=new MyRun();
myRun.start();
}
案例实现Runnable接口
class MyRun2 implements Runnable
{
@Override
public void run() {
for (int i=1;i<=100;i++)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("hzj",i+"----");
}
}
}
public void btn1(View view)
{
MyRun2 myRun2=new MyRun2();
Thread thread=new Thread(myRun2);
thread.start();
}
在JAVA中线程对象为Thread,因此Thread有很多方法便于我们管理线程