测试如下
package com.cky.prioritydemo; /** * Created by edison on 2017/12/3. */ public class ThreadA extends Thread{ private int count =0; public int getCount() { return count; } @Override public void run() { super.run(); while(true) { count++; } } }
package com.cky.prioritydemo; /** * Created by edison on 2017/12/3. */ public class ThreadB extends Thread{ private int count =0; public int getCount() { return count; } @Override public void run() { super.run(); while(true) { count++; } } }
package com.cky.prioritydemo; /** * Created by edison on 2017/12/3. */ public class TestAB { public static void main(String[] args) { try { ThreadA thA = new ThreadA(); thA.setPriority(Thread.NORM_PRIORITY -3); thA.start(); ThreadB thB = new ThreadB(); thB.setPriority(Thread.NORM_PRIORITY +3); thB.start(); Thread.sleep(2000); thA.stop(); thB.stop(); System.out.println("a="+thA.getCount()); System.out.println("b="+thB.getCount()); } catch (InterruptedException e) { e.printStackTrace(); } } }
a=1428639097
b=1429717263