线程是发出先级分开的。在java中装置线程优先级应用setPriority

当操作系统中,线程是来先级分开的,优先级较高之线程会获得相对较多的资源。

java多线程系列8-线程的优先级,多线程8-

以java中装置线程优先级应用setPriority,在jdk中的源代码如下:

public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }

于java中,线程的先期级分为1~10当即10只级次,小于1要么过10,则会弃来IllegalArgumentException异常

在JDK中使用3单常量来预定义优先级:

  /**
     * The minimum priority that a thread can have.
     */
    public final static int MIN_PRIORITY = 1;

   /**
     * The default priority that is assigned to a thread.
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10;

也就是说CPU会先行履优先级较高之线程对象吃之职责。

线程优先级的继承性

于java中,线程的优先级有继承性,例如A线程启动B线程,则A和B的预级是平等的。举个例子:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread1 run priority=" + this.getPriority());
        MyThread2 thread2 = new MyThread2();
        thread2.start();
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread2 run priority=" + this.getPriority());
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main thread begin priority="
                + Thread.currentThread().getPriority());
        System.out.println("main thread end priority="
                + Thread.currentThread().getPriority());
        MyThread1 thread1 = new MyThread1();
        thread1.start();
    }
}

运转结果如下:

main thread begin priority=5
main thread end priority=5
MyThread1 run priority=5
MyThread2 run priority=5

安线程优先级有助于帮助“线程规划器”确定下次选取啊一个线程来优先执行。

先级有规则性

虽setPriority()方法可以安装线程的优先级,但是没有看出设置优先级所带的功力

比喻如下:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult += j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 1 use time=" + (endTime - beginTime));
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult += j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 2 use time=" + (endTime - beginTime));
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {

        for(int i=0;i<5;i++){
            MyThread1 thread1=new MyThread1();
            thread1.setPriority(10);
            thread1.start();
            MyThread2 thread2 =new MyThread2();
            thread2.setPriority(1);
            thread2.start();
        }
    }
}

运转结果如下:

* * * * * thread 1 use time=174
* * * * * thread 1 use time=221
* * * * * thread 1 use time=224
* * * * * thread 2 use time=360
* * * * * thread 1 use time=202
* * * * * thread 2 use time=185
* * * * * thread 1 use time=169
* * * * * thread 2 use time=466
* * * * * thread 2 use time=425
* * * * * thread 2 use time=98

打地方的结果好视,高优先级的线程总是大部分预实施了,但是未是富有的先期实行完毕。先实行完毕也非是因事先调用,如果转优先级,先实行完毕和同代码的调用顺序无关。

事先级有一定之规则性,CPU总是尽可能以尽资源让优先级比较高之线程

安装线程的先行级应用setPriority()方法,此道以JDK的源代码如下:

事先级有随机性

优先级较高的线程不必然每一样差都先实施完毕,举个例证:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 1 use time="
                + (endTime - beginTime));
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 2 use time="
                + (endTime - beginTime));
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            MyThread1 thread1 = new MyThread1();
            thread1.setPriority(5);
            thread1.start();
            MyThread2 thread2 = new MyThread2();
            thread2.setPriority(6);
            thread2.start();
        }
    }
}

运转结果如下:

* * * * * thread 1 use time=5
* * * * * thread 2 use time=4
* * * * * thread 1 use time=5
* * * * * thread 1 use time=4
* * * * * thread 1 use time=4
* * * * * thread 2 use time=6
* * * * * thread 2 use time=3
* * * * * thread 2 use time=5
* * * * * thread 1 use time=2
* * * * * thread 2 use time=6

线程的优先级与打印顺序无关,它们的涉嫌具有无明显和随机性

相关文章
java多线程系列8-线程的优先级
java多线程系列7-停止线程
java多线程系列6-阻塞队列
java多线程系列5-死锁与线程间通信
java多线程系列4-线程池
java多线程系列3-线程同步
java多线程系列2-线程控制
java多线程系列1--线程实现与调度

http://www.bkjia.com/Javabc/1126437.htmlwww.bkjia.comtruehttp://www.bkjia.com/Javabc/1126437.htmlTechArticlejava多线程系列8-线程的优先级,多线程8-
在java中安线程优先级应用setPriority,在jdk中之源代码如下: public
final void setPriority( int newPriori…

    public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }

于java中,线程的先行级分为1~10及时10单优先级,如果低于1要超越10,则JDK抛来非常IllegalArgumentException()。

图片 1

JDK常用脚三单量来预置定义优先级的值。

图片 2

1.10.1线程优先级的继承特性

在java中线程的预先级有继承性,比如A线程启动B线程,则B线程的优先级与A是同的。

线程代码1:

public class Thread1 extends Thread {
    @Override
    public void run() {
        System.out.println("Thread1 run priority = " + this.getPriority());
        Thread2 thread = new Thread2();
        thread.start();
    }
}

线程代码2:

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

行代码:

public class Main {
    public static void main(String[] args) {
        System.out.println("Main thread begin priority = " + Thread.currentThread().getPriority());
        //Thread.currentThread().setPriority(6);
        System.out.println("main thread end priority = " + Thread.currentThread().getPriority());
        Thread1 thread1 = new Thread1();
        thread1.start();
    }
}

实行结果(左为产生注释,右为没有注释):

图片 3图片 4

 

1.10.2优先级有规则性:

线程代码1:

public class Thread3 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++){
                Random random = new Random();
                random.nextInt();
                addResult = addResult + j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("thread 3 use time = " + (endTime - beginTime));
    }
}

线程代码2:

public class Thread4 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++){
                Random random = new Random();
                random.nextInt();
                addResult = addResult + j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("thread 4 use time = " + (endTime - beginTime));
    }
}

施行代码:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            Thread3 thread3 = new Thread3();
            thread3.setPriority(10);
            thread3.start();
            Thread4 thread4 = new Thread4();
            thread4.setPriority(1);
            thread4.start();
        }
    }
}

施行结果:

图片 5图片 6图片 7

走了反复继,会发觉优先的线程会先行实施了。

 实际上线程的行各个和线程代码的实行顺序无关,与线程的优先级有关,优先级更强越先实行。

1.10.3先级有随机性:

随机性意味着优先级赛的线程不必然总是能够事先执行了。

线程代码1:

public class Thread5 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("thread 5 use time = " + (endTime - beginTime));
    }
}

 

线程代码2:

public class Thread6 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("thread 6 use time = " + (endTime - beginTime));
    }
}

 

履代码:

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            Thread5 thread5 = new Thread5();
            thread5.setPriority(5);
            thread5.start();
            Thread6 thread6 = new Thread6();
            thread6.setPriority(6);
            thread6.start();
        }
    }
}

 

 执行结果:

图片 8

自打夫结果来拘禁线程的先期级有随机性,不必然优先级赛之就算定先实行完毕。

 1.10.4禁闭哪个运行的不久:

线程代码1:

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

    public int getCount() {
        return count;
    }

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

 

线程代码2:

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

    public int getCount() {
        return count;
    }

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

 

实施线程:

public class Main {
    public static void main(String[] args) {
        try {
            ThreadA a = new ThreadA();
            a.setPriority(Thread.NORM_PRIORITY - 3);
            a.start();
            ThreadB b = new ThreadB();
            b.setPriority(Thread.NORM_PRIORITY + 3);
            b.start();
            Thread.sleep(10000);
            a.stop();
            b.stop();
            System.out.println("a = " + a.getCount());
            System.out.println("b = " + b.getCount());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 执行结果:

图片 9

先期级赛之线程,执行进度还快。

 

 

源码地址:https://github.com/lilinzhiyu/threadLearning

正文内容是开中情节有自己的村办见解所成。可能于个人看法及会见时有发生过多题目(毕竟知识量有限,导致认知也简单),如果读者认为有题目要大胆提出,我们得相互交流、相互学习,欢迎你们的赶到,心成意足,等待你的褒贬。