public class MyThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 100; i++) { System.out.println(Thread.currentThread().getName() + i); Thread.yield(); // 让出CPU } } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub MyThread myThread = new MyThread(); Thread thread = new Thread(myThread); thread.setPriority(Thread.MAX_PRIORITY); thread.start(); for (int i = 0; i < 100; i++) { System.out.println("main " + i); } } }
运行结果
main 0 main 1 main 2 main 3 main 4 Thread-00 main 5 Thread-01 main 6 Thread-02 main 7