マルチスレッドを最初から認識-1.16異なる優先度を比較
4093 ワード
この章では、異なる優先度がどのような結果をもたらすかを比較するテストを行います.
1.コードリスト
出力:
high priority 468high priority 484low priority 406low priority 656low priority 609low priority 750low priority 656high priority 515high priority 813high priority 547--------------high priority 438high priority 453high priority 484high priority 515high priority 438low priority 578low priority 484low priority 703low priority 610low priority 641--------------low priority 375high priority 531low priority 578low priority 594high priority 593low priority 703high priority 687low priority 468high priority 750high priority 672--------------low priority 343high priority 360low priority 343high priority 562high priority 390low priority 250high priority 485low priority 500high priority 234low priority 234--------------high priority 140high priority 297high priority 156high priority 188high priority 109low priority 406low priority 468low priority 484low priority 359low priority 500
第2グループの出力から、最後のグループと同じで、しばらくなぜか分からないが、後で解決しなければならない.
第3のグループの出力から,優先度が高いとは限らず先に完了できることが分かった.
最後のグループの出力から,大きな優先度の差が実行順序の違いをもたらすことが分かった.
まとめ:この章では、主に異なる優先順位でプログラムを実行する効果を比較します.
私のgithub:https://github.com/raylee2015/DeepIntoThread
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 468high priority 484low priority 406low priority 656low priority 609low priority 750low priority 656high priority 515high priority 813high priority 547--------------high priority 438high priority 453high priority 484high priority 515high priority 438low priority 578low priority 484low priority 703low priority 610low priority 641--------------low priority 375high priority 531low priority 578low priority 594high priority 593low priority 703high priority 687low priority 468high priority 750high priority 672--------------low priority 343high priority 360low priority 343high priority 562high priority 390low priority 250high priority 485low priority 500high priority 234low priority 234--------------high priority 140high priority 297high priority 156high priority 188high priority 109low priority 406low priority 468low priority 484low priority 359low priority 500
第2グループの出力から、最後のグループと同じで、しばらくなぜか分からないが、後で解決しなければならない.
第3のグループの出力から,優先度が高いとは限らず先に完了できることが分かった.
最後のグループの出力から,大きな優先度の差が実行順序の違いをもたらすことが分かった.
まとめ:この章では、主に異なる優先順位でプログラムを実行する効果を比較します.
私のgithub:https://github.com/raylee2015/DeepIntoThread