public void run() { // TODO Auto-generated method stub while (keepRunning) { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + "进攻了" + i); } // Thread.yield(); } System.out.println(Thread.currentThread().getName() + "进攻结束!"); }
当我把yield放在for循环外,或者是注释屏蔽,两个军队线程依旧会是混战,连击,yield要不要效果都一样
它只是让当前线程进入就绪状态。调用后所有线程重新抢夺资源