码迷,mamicode.com
首页 > 编程语言 > 详细

1.10.2线程的优先级的规则性

时间:2017-12-03 19:03:02      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:++   线程   jdk   优先   com   random   rand   cpu   led   

测试

package com.cky.prioritydemo;

import java.util.Random;

/**
 * Created by edison on 2017/12/3.
 */
public class MyThread1 extends  Thread{
    @Override
    public void run() {
        super.run();
        long begin = System.currentTimeMillis();
        long addResult =0 ;
        for (int j = 0; j <10 ; j++) {
            for (int i = 0; i <50000 ; i++) {
                Random random = new Random();
                random.nextInt();
                addResult = addResult + 1;
            }
        }

        long end = System.currentTimeMillis();
        System.out.println("thread 1 use time="+(end-begin));
    }
}
 1 package com.cky.prioritydemo;
 2 
 3 import java.util.Random;
 4 
 5 /**
 6  * Created by edison on 2017/12/3.
 7  */
 8 public class MyThread2 extends  Thread{
 9     @Override
10     public void run() {
11         super.run();
12         long begin = System.currentTimeMillis();
13         long addResult =0 ;
14         for (int j = 0; j <10 ; j++) {
15             for (int i = 0; i <50000 ; i++) {
16                 Random random = new Random();
17                 random.nextInt();
18                 addResult = addResult + 1;
19             }
20         }
21 
22         long end = System.currentTimeMillis();
23         System.out.println("thread 2 use time="+(end-begin));
24     }
25 }
package com.cky.prioritydemo;

/**
 * Created by edison on 2017/12/3.
 */
public class PriorityTest {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            MyThread1 th1 = new MyThread1();
            th1.setPriority(10);
            th1.start();
            MyThread2 th2 = new MyThread2();
            th2.setPriority(1);
            th2.start();
        }
    }
}
C:\itsoft\jdk\bin\java -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.prioritydemo.PriorityTest
thread 1 use time=344
thread 1 use time=438
thread 1 use time=438
thread 1 use time=453
thread 1 use time=485
thread 2 use time=516
thread 2 use time=516
thread 2 use time=547
thread 2 use time=547
thread 2 use time=547

Process finished with exit code 0

高优先级的线程总是大部分先执行完,但不代表高优先级的线程全部先执行完,而且当线程的优先级等级差距很大时,谁先执行完和代码调用顺序无关,测试如下

 1 package com.cky.prioritydemo;
 2 
 3 /**
 4  * Created by edison on 2017/12/3.
 5  */
 6 public class PriorityTest {
 7     public static void main(String[] args) {
 8         for (int i = 0; i < 5; i++) {
 9             MyThread1 th1 = new MyThread1();
10             th1.setPriority(1);
11             th1.start();
12             MyThread2 th2 = new MyThread2();
13             th2.setPriority(10);
14             th2.start();
15         }
16     }
17 }

C:\itsoft\jdk\bin\java -Didea.launcher.port=7541 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.prioritydemo.PriorityTest
thread 2 use time=297
thread 2 use time=313
thread 2 use time=328
thread 1 use time=438
thread 2 use time=485
thread 1 use time=485
thread 2 use time=500
thread 1 use time=500
thread 1 use time=516
thread 1 use time=516

Process finished with exit code 0

结果分析:大部分挺好read先执行完,说明线程的优先级具有一定的规则性,也就是cpu进行执行优先级高得线程

1.10.2线程的优先级的规则性

标签:++   线程   jdk   优先   com   random   rand   cpu   led   

原文地址:http://www.cnblogs.com/edison20161121/p/7954797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!