标签:dom art sleep deepin i++ oid rtti sni github
这一章节我们来做一个測试,对照一下不同的优先级会形成如何的结果?
1.代码清单
package com.ray.deepintothread.ch01.topic_16; import java.util.Random; public class PrioritySample2 { public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 5; i++) { ThreadThree ThreadThree = new ThreadThree(); ThreadThree.setName("high priority"); ThreadThree.setPriority(2); ThreadThree.start(); ThreadFour ThreadFour = new ThreadFour(); ThreadFour.setName("low priority"); ThreadFour.setPriority(1); ThreadFour.start(); } Thread.sleep(2000); System.out.println("--------------"); for (int i = 0; i < 5; i++) { ThreadThree ThreadThree = new ThreadThree(); ThreadThree.setName("high priority"); ThreadThree.setPriority(3); ThreadThree.start(); ThreadFour ThreadFour = new ThreadFour(); ThreadFour.setName("low priority"); ThreadFour.setPriority(2); ThreadFour.start(); } Thread.sleep(2000); System.out.println("--------------"); for (int i = 0; i < 5; i++) { ThreadThree ThreadThree = new ThreadThree(); ThreadThree.setName("high priority"); ThreadThree.setPriority(6); ThreadThree.start(); ThreadFour ThreadFour = new ThreadFour(); ThreadFour.setName("low priority"); ThreadFour.setPriority(5); ThreadFour.start(); } Thread.sleep(2000); System.out.println("--------------"); for (int i = 0; i < 5; i++) { ThreadThree ThreadThree = new ThreadThree(); ThreadThree.setName("high priority"); ThreadThree.setPriority(8); ThreadThree.start(); ThreadFour ThreadFour = new ThreadFour(); ThreadFour.setName("low priority"); ThreadFour.setPriority(7); ThreadFour.start(); } Thread.sleep(2000); System.out.println("--------------"); for (int i = 0; i < 5; i++) { ThreadThree ThreadThree = new ThreadThree(); ThreadThree.setName("high priority"); ThreadThree.setPriority(10); ThreadThree.start(); ThreadFour ThreadFour = new ThreadFour(); ThreadFour.setName("low priority"); ThreadFour.setPriority(1); ThreadFour.start(); } } } class ThreadThree extends Thread { @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { Random random = new Random(); random.nextInt(50); } long endTime = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName() + " " + (endTime - startTime)); } } class ThreadFour extends Thread { @Override public void run() { long startTime = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { Random random = new Random(); random.nextInt(50); } long endTime = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName() + " " + (endTime - startTime)); } }
high priority 468
high priority 484
low priority 406
low priority 656
low priority 609
low priority 750
low priority 656
high priority 515
high priority 813
high priority 547
--------------
high priority 438
high priority 453
high priority 484
high priority 515
high priority 438
low priority 578
low priority 484
low priority 703
low priority 610
low priority 641
--------------
low priority 375
high priority 531
low priority 578
low priority 594
high priority 593
low priority 703
high priority 687
low priority 468
high priority 750
high priority 672
--------------
low priority 343
high priority 360
low priority 343
high priority 562
high priority 390
low priority 250
high priority 485
low priority 500
high priority 234
low priority 234
--------------
high priority 140
high priority 297
high priority 156
high priority 188
high priority 109
low priority 406
low priority 468
low priority 484
low priority 359
low priority 500
从第二组输出看出。跟最后一组同样,临时不知道为什么,有待以后解决
从第三组输出我们能够观察到,不一定是优先级高的就能够先完毕
从最后一组的输出我们能够看到。巨大的优先级差距带来的是运行顺序的不一样
总结:这一章节我们主要对照一下不同优先级运行程序的效果是如何的
标签:dom art sleep deepin i++ oid rtti sni github
原文地址:http://www.cnblogs.com/lytwajue/p/7281729.html