闪酷跑路网

一行行枯燥的代码,却描绘出人生的点点滴滴

您现在的位置是:首页>_综合

从头认识多线程-1.16 对照不同的优先级

发布时间:2018-12-15浏览(4970)

    从头认识多线程-1.16 对比不同的优先级

    这一章节我们来做一个测试,对比一下不同的优先级会形成怎样的结果?

    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


    从第二组输出看出,跟最后一组相同,暂时不知道为什么,有待以后解决

    从第三组输出我们可以观察到,不一定是优先级高的就能够先完成

    从最后一组的输出我们可以看到,巨大的优先级差距带来的是执行顺序的不一样


    总结:这一章节我们主要对比一下不同优先级执行程序的效果是怎样的


    我的github:https://github.com/raylee2015/DeepIntoThread