线程用到Thread或者Runnable接口(Thread也操作了Runnable接口)
继承了Thread类后需要重载其run方法,在方法里写你需要完成的事情,开始线程是调用其start方法。
操作Runnable接口必须实现其run方法,在方法里写你需要完成的事情,Runnable接口没有start方法,所以启动线程还是需要依靠Thread类 new Thread(Runnable runnable).start();
一般项目中多是操作接口,因为类只能单继承,接口可以操作多个。
packagecom.accp.wangyou;importjava.util.Arrays;publicclasstest{finalint[]arr={90,81,78,95,79,72,85};publicstaticvoidmain(String[]args){newThread(newtest().newrunnable1()).start();newThread(newtest().newrunnable2()).start();newThread(newtest().newrunnable3()).start();}classrunnable1implementsRunnable{publicvoidrun(){intavg=0;for(inti=0;i
package com;
public class MyThread extends Thread{
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);//休眠1秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("HelloWorld!!!");
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
http://v.youku.com/v_show/id_XMjE0NTkxMzcy.html
绝对受用