package charpter07;
//yield():礼让的行为
public class Processor implements Runnable {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "--------->" + i);
// 让线程发生礼让的行为
if (i % 5 == 0) {
Thread.yield();
}
}
}
}
------------------------
package charpter07;
public class TestYield {
public static void main(String[] args) {
// 创建p对象
Processor p = new Processor();
// 创建线程并传值给构造方法
Thread t1 = new Thread(p, "A");
// 创建线程并传值给构造方法
Thread t2 = new Thread(p, "B");
// 用对象调用方法
t1.start();
t2.start();
}
}