线程优先级

线程的优先级

在操作系统中,线程可以划分优先级,Cpu优先执行优先级高的线程的任务。

在java中线程优先级分为1~10,如果小于1或者大于10,则jdk报illegalArgumentException()异常。

设置线程优先级使用setPriority()方法。

1、线程优先级具有继承性。a线程启动b线程,b线程的优先级和a线程的优先级是一样的。

public class MyThread extends Thread {
   
    @Override
    public void run() {
        System.out.println("mythread1 run priority is"+this.getPriority());
        MyThread2 myThread2 = new MyThread2();
        myThread2.start();

    }

}
public class MyThread2 extends Thread {
  
    @Override
    public void run() {
        System.out.println("mythread2 run priority is"+this.getPriority());

    }
}
public class TestMyThread {
    public static void main(String[] args) {
        System.out.println("main线程的优先级"+Thread.currentThread().getPriority());
        Thread.currentThread().setPriority(6);
        MyThread thread = new MyThread();
        thread.start();
        System.out.println("main执行结束");
    }
}

运行结果是:

 

2、线程具有规则性。

高优先级的线程总是大部分先执行完,并不是高优先级的完全先执行完。线程的优先级和执行顺序无关。出现这样的结果证明mythread2的优先级是最高的,说明线程的优先级具有一定的规则性,cpu尽量将执行资源让给优先级比较高的线程。

public class TestMyThread {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            MyThread myThread1 = new MyThread();
            myThread1.setPriority(1);
            myThread1.start();
            MyThread2 myThread2 = new MyThread2();
            myThread1.setPriority(10);
            myThread2.start();
        }
    }
}
public class MyThread extends Thread {
    
    @Override
    public void run() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {

        }
        System.out.println("thread1-----" + (System.currentTimeMillis() - start));

    }
}
public class MyThread2 extends Thread {
  
    @Override
    public void run() {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {

        }
        System.out.println("thread2-----" + (System.currentTimeMillis() - start));

    }

}

优先级高的线程执行快。

public class MyThread extends Thread {
    private int count = 0;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void run() {
        while (true){
            count++;
        }

    }
}
public class MyThread2 extends Thread {
    private long count = 0;

    public MyThread2() {
        super();
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    @Override
    public void run() {
        while (true){
            count++;
        }

    }
}
public class TestMyThread {
    //
    public static void main(String[] args) {
        try {

            MyThread myThread1 = new MyThread();
            myThread1.setPriority(Thread.NORM_PRIORITY - 3);
            myThread1.start();
            MyThread2 myThread2 = new MyThread2();
            myThread2.setPriority(Thread.NORM_PRIORITY + 3);
            myThread2.start();
            Thread.sleep(10000);
            myThread1.stop();
            myThread2.stop();
            System.out.println("thread1.count" + myThread1.getCount());
            System.out.println("thread2.count" + myThread2.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

运行结果:

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值