关于yield()方法;
public class YieldTest {
public static void main(String[] args) {
Thread t1=new MyThreadG();
Thread t2=new MyThreadH();
t1.start();
t2.start();
}
}
class MyThreadG extends Thread{
public void run(){
int i=0;
while(i<100){
i++;
for(int j=0;j<100000;j++)
;
System.out.println(i);
if(i%10==0){
Thread.yield();//放弃CPU执行权
}
}
}
}
class MyThreadH extends Thread{
public void run(){
char c='A'-1;
while(c<'Z'){
c++;
for(int i=0;i<100000;i++)
;
System.out.println(c);
}
}
}
输入的结果怎么不对,应该是每隔10个输出一个字母吧,可是为什么结果不是啊!
程序对的,为什么呢,是不是跟电脑CPU有关啊!