public class ThreadTest3 {
public static void main(String[] args) {
Runnable r=new HelloThread();
Thread t1=new Thread(r);
//r=new HelloThread();
Thread t2=new Thread(r);
t1.start();
t2.start();
}
}
class HelloThread implements Runnable
{
int i; //成员变量
@Override
public void run() {
int i=0; //局部变量
while(true){
System.out.println(“number:”+this.i++); //调用的是成员变量i,此时无止境的执行下去
try {
Thread.sleep((long) (Math.random()*1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i==50){
break;
}
}
}
}