测试如下

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

 

相关文章:

猜你喜欢
  • 2021-05-30
  • 2021-04-25
  • 2022-01-10
  • 2021-05-31
  • 2021-11-21
  • 2021-12-23
相关资源
相似解决方案