public class Comity {
public static void main(String[] args) {
ThreadYield threadYield = new ThreadYield();
ThreadYield threadYield2 = new ThreadYield();
threadYield.setName("55开");
threadYield2.setName("zzzz");
threadYield.start();
threadYield2.start();
}
}
class ThreadYield extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName() + "------" + i);
yield();
}
}
}
55开------0
zzzz------0
55开------1
55开------2
zzzz------1
55开------3
55开------4
zzzz------2
zzzz------3
zzzz------4
55开------5
zzzz------5
zzzz------6
55开------6
zzzz------7
55开------7
zzzz------8
55开------8
55开------9
zzzz------9