PRIORITY of Thread

来源:互联网 发布:永恒战士3mac版 编辑:程序博客网 时间:2024/05/22 05:13

public class TestJoin {

 public static void main(String[] args) {
  MyThread myThread = new MyThread("one");
  MyThread otherThread = new MyThread("other");
  myThread.setPriority(Thread.NORM_PRIORITY+4);
  myThread.start();
  otherThread.start();
 }

}

class MyThread extends Thread {
 public MyThread(String s) {
  super(s);
 }

 public void run() {
  for (int i = 0; i < 10; i++) {
   System.out.println("I am" + getName()+" " + i);

   Thread.yield();

  }
 }

}

Result:

I amone 0
I amother 0
I amone 1
I amone 2
I amone 3
I amone 4
I amone 5
I amone 6
I amone 7
I amone 8
I amone 9
I amother 1
I amother 2
I amother 3
I amother 4
I amother 5
I amother 6
I amother 7
I amother 8
I amother 9

结论:one线程的优先级高于other,但并不是other线程没有执行的机会,而是拥有的时间片少于one的

原创粉丝点击