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

Posted lytwajue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从头认识多线程-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

以上是关于从头认识多线程-1.16 对照不同的优先级的主要内容,如果未能解决你的问题,请参考以下文章

从头认识Spring-1.16 SpEl对集合的操作-查询集合以及投影元素集合

从头认识Spring-1.16 SpEl对集合的操作-建立集合以及访问集合的元素,以<util:list/>为例

从头认识多线程-3.1 使用volatile解决异步死循环

从头认识java-14.1 再次对照数组与容器

从头认识多线程-1.9 迫使线程停止的方法-return法

从头认识多线程-1.8 迫使线程停止的方法-暴力Stop方法