PRIORITY of Thread

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的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值