Testing thread priority. How come in some cases low priority threads are faster?

懵懂的女人 提交于 2020-01-11 10:15:30

问题


I'm trying to test 2 threads, one with high, and the other with low priority.

According to my results sometimes the low priority thread is faster, how is this possible? I've tested the different priority threads by increment a click variable inside each thread. I've also increased and decreased the sleep time, but nothing.

Since I was testing with no heavy programs running in the background, I decided to test with an HD movie running, but still no real change, threads are always the same speed.

My PC is an Intel i5. I'm running Windows 7 64bit, 16GB RAM

This is the code:

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);
 }  
}

回答1:


You have two problems. One is that threads take a while to start, so you're giving "Low" a pretty good head start by firing them off serially. The other is that thread priority decides who gets to run when there's an argument for processor time. With two threads and 8 effective processor cores, priority isn't going to matter a whole lot! Here is a fixed example that uses a latch to start all threads as "simultaneously" and uses enough threads that they actually fight over resources and you can see the effect of priority settings. It gives pretty consistent results.

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);
 }  



回答2:


Thread priorities are not guaranteed to have any effect; this is mentioned in multiple places, including JDK javadocs. So assuming that on platform you are running on basically ignores levels, then it goes back to basic stastical probabilities: sometimes some threads seem to run faster than others, depending on how scheduler works and so on.

I don't think anyone really uses Java thread priorities for everything, given that their working (or lack thereof) is at best platform-dependent.



来源:https://stackoverflow.com/questions/16289637/testing-thread-priority-how-come-in-some-cases-low-priority-threads-are-faster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!