public class TestThread {
public static void main(String args[]){
Runner1 r = new Runner1();
r.start();
r.
//Thread t = new Thread(r);
//t.start();
for(int i=0; i<100; i++){
System.out.println("Main Thread:--------" + i);
}
}
}
//class Runner1 implements Runnable{ //只要能使用接口就使用接口, 到时候还可以扩展继承,
class Runner1 extends Thread{
//继承比较死,你从这里继承了,其他类就不能从这里继承了, java是单向继承的。。现在计划生育都搞到编程语言来了。 接口难道是情人?
public void run(){
for(int i=0; i<100; i++){
System.out.println("Runner1 :" + i);
}
}
}
public class TestInterrupt {
public static void main(String[] args){
MyThread thread = new MyThread();
thread.start();
try{Thread.sleep(10000);} //10秒钟
catch(InterruptedException e){}
thread.interrupt(); //interrupt 中断线程。 相当于你睡着了,泼的一盆凉水
}
}
class MyThread extends Thread {
boolean flag = true; //等于false 会不能运行
public void run(){
while(false){ //while(true)是死循环
System.out.println("***" +new Date() + "***");
try{
sleep(1000); //每隔一秒打印当前时间
}catch(InterruptedException e){
return;
}
}
}
}
下面输出: