测试线程优先级。在某些情况下,低优先级线程如何更快?

时间:2013-04-29 23:01:18

标签: java multithreading thread-priority

我正在尝试测试2个线程,一个是高线程,另一个是低优先级。

根据我的结果,有时低优先级线程更快,这怎么可能? 我通过在每个线程内增加一个click变量来测试不同的优先级线程。 我也增加和减少了睡眠时间,但没有。

由于我测试的是没有重型程序在后台运行,我决定测试运行的高清电影,但仍然没有真正的改变,线程总是相同的速度。

我的电脑是英特尔i5。我正在运行Windows 7 64位,16GB RAM

这是代码:

class clicker implements Runnable{
    long click =0;
    Thread t;
    private volatile boolean running = true;

    clicker(int p){
        t=new Thread(this);
        t.setPriority(p);
    }

    public void run(){
        while(running)
            click++;
    }

    public void stop(){
        running = false;
    }

    public void start(){
        t.start();
    }
}




class HiLoPri {
public static void main(String args[]){
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    clicker hi=new clicker(Thread.NORM_PRIORITY+4);
    clicker lo=new clicker(Thread.NORM_PRIORITY-4);

    lo.start();
    hi.start();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    lo.stop();
    hi.stop();

    try {
        hi.t.join();
        lo.t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("LO: "+lo.click);
    System.out.println("HI: "+hi.click);
 }  
}

2 个答案:

答案 0 :(得分:3)

你有两个问题。一个是线程需要一段时间才能开始,所以你通过连续发射它们给予“低”一个很好的开端。另一个是线程优先级决定在有处理器时间参数时谁运行。有两个线程和8个有效的处理器内核,优先级并不重要!下面是一个固定的例子,它使用一个latch来“启动”所有线程“同时”并使用足够的线程来实际对战资源进行争夺,你可以看到优先级设置的效果。它提供了非常一致的结果。

static class Clicker implements Runnable{
    BigInteger click = BigInteger.ZERO;
    Thread t;

    Clicker(int p){
        t=new Thread(this);
        t.setPriority(p);
    }

    public void run(){
        try {
        latch.await();
        } catch(InterruptedException ie) {}
        while(running)
            click = click.add(BigInteger.ONE);
    }

    public void start(){
        t.start();
    }
}

public static volatile boolean running = true;
public static final CountDownLatch latch = new CountDownLatch(1);

public static void main(String args[]){
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    List<Clicker> listLow = new ArrayList<Clicker>();
    List<Clicker> listHigh = new ArrayList<Clicker>();
    for (int i = 0; i < 16; i++) {
        listHigh.add(new Clicker(Thread.NORM_PRIORITY+4));
    }
    for (int i = 0; i < 16; i++) {
        listLow.add(new Clicker(Thread.NORM_PRIORITY-4));
    }
    for (Clicker clicker: listLow) {
        clicker.start();
    }
    for (Clicker clicker: listHigh) {
        clicker.start();
    }
    latch.countDown();
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {

        e.printStackTrace();
    }
    running = false;

    BigInteger lowTotal = BigInteger.ZERO;
    BigInteger highTotal = BigInteger.ZERO;
    try {
        for (Clicker clicker: listLow) {
            clicker.t.join();
            lowTotal = lowTotal.add(clicker.click);
        }
    for (Clicker clicker: listHigh) {
            clicker.t.join();
            highTotal = highTotal.add(clicker.click);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("LO: "+lowTotal);
    System.out.println("HI: "+highTotal);
 }  

答案 1 :(得分:2)

不保证线程优先级有任何影响;这在多个地方都有提到,包括JDK javadocs。因此,假设您在平台上运行时基本上忽略了级别,那么它会回到基本的统计概率:有时某些线程似乎比其他线程运行得更快,具体取决于调度程序的工作方式等等。

我认为任何人都不会真正使用Java线程优先级,因为他们的工作(或缺乏)最多取决于平台。