测试线程优先级。在某些情况下,低优先级线程如何更快? [英] Testing thread priority. How come in some cases low priority threads are faster?

查看:189
本文介绍了测试线程优先级。在某些情况下,低优先级线程如何更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

My PC is an Intel i5. I'm running Windows 7 64bit, 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);
 }  
}


推荐答案

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

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

这篇关于测试线程优先级。在某些情况下,低优先级线程如何更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆