国际贸易运输计算:解释一下这是为什么

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 12:09:48
看这个程序
public class tth{
public static void main(String args[]){
Thread t1=new Thread(new runner(1));
Thread t2=new Thread(new runner(2));
Thread t3=new Thread(new runner(3));
t1.start();
t2.start();
t3.start();
}
}
class runner implements Runnable{
int id;
runner(int id){
this.id=id;}
public void run(){
for(int i=0;i<20;i++){
if(i%5==0) System.out.print("\r");
Thread.currentThread().yield();
System.out.print(id);
}
}
}
为什么这个程序第一次运行是231231231231231 后面不管你运行多少次都是123123123123123
我当然知道这是个循环啊 问题是为什么第一次运行就不出来123123123123123 而是231231231231231 而以后在运行的话就是123123123123123

原因就在:
int id;
runner(int id){
this.id=id;}
public void run(){
for(int i=0;i<20;i++){
if(i%5==0) System.out.print("\r");
Thread.currentThread().yield();
它是一个循环啊!!!