多线程学习--线程优先级

你的名字 2023-10-27 06:29 248阅读 0赞

线程的优先级:继承性,规则性,随机性

  • 线程的优先级具有继承性. 如,线程A启动线程B,则B和A优先级一样
  • 线程的优先级具有规则性. CPU尽量倾向于把资源优先级高的线程
  • 线程的优先级具有随机性. 优先级不等同于执行顺序,二者关系不确定

优先级范围:0~10

继承性:

  1. class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. System.out.println("MyThread17_1 run priority=" + this.getPriority());
  5. MyThread2 myThread2 = new MyThread2 ();
  6. myThread2 .start();
  7. }
  8. }
  9. class MyThread2 extends Thread {
  10. @Override
  11. public void run() {
  12. System.out.println("MyThread17_2 run priority=" + this.getPriority());
  13. }
  14. }
  15. public class test{
  16. public static void main(String[] args) {
  17. System.out.println("main thread begin priority =" + Thread.currentThread().getPriority());
  18. //Thread.currentThread().setPriority(6);
  19. System.out.println("main thread end priority =" + Thread.currentThread().getPriority());
  20. MyThread myThread = new MyThread ();
  21. myThread .start();
  22. }
  23. }

结果:

  1. //Thread.currentThread().setPriority(6);
  2. 输出:
  3. main thread begin priority =5
  4. main thread end priority =5
  5. MyThread run priority=5
  6. MyThread2 run priority=5
  7. --------------------
  8. Thread.currentThread().setPriority(6);
  9. 输出:
  10. main thread begin priority =5
  11. main thread end priority =6
  12. MyThread run priority=6
  13. MyThread2 run priority=6
  14. 规则性:
  15. class MyThread extends Thread {
  16. @Override
  17. public void run() {
  18. long beginTime = System.currentTimeMillis();
  19. long addResult = 0;
  20. for (int j = 0; j < 10; j++) {
  21. for (int i = 0; i < 50000; i++) {
  22. Random random = new Random();
  23. random.nextInt();
  24. addResult += i;
  25. }
  26. }
  27. long endTime = System.currentTimeMillis();
  28. System.out.println("★★★★★ thread1 use time = " + (endTime - beginTime));
  29. }
  30. }
  31. class MyThread2 extends Thread {
  32. @Override
  33. public void run() {
  34. long beginTime = System.currentTimeMillis();
  35. long addResult = 0;
  36. for (int j = 0; j < 10; j++) {
  37. for (int i = 0; i < 50000; i++) {
  38. Random random = new Random();
  39. random.nextInt();
  40. addResult += i;
  41. }
  42. }
  43. long endTime = System.currentTimeMillis();
  44. System.out.println("☆☆☆☆☆ thread2 use time = " + (endTime - beginTime));
  45. }
  46. }
  47. public class Run18_priority02 {
  48. public static void main(String[] args) {
  49. for (int i = 0; i < 5; i++) {
  50. MyThread myThread = new MyThread ();
  51. myThread .setPriority(10);
  52. //myThread .setPriority(1);
  53. myThread .start();
  54. MyThread2 myThread2 = new MyThread2 ();
  55. //myThread2 .setPriority(10);
  56. myThread2 .setPriority(1);
  57. myThread2 .start();
  58. }
  59. }
  60. }

结果:

  1. myThread.setPriority(10);
  2. myThread2.setPriority(1);
  3. 输出:
  4. ☆☆☆☆☆ thread2 use time = 202
  5. ★★★★★ thread1 use time = 208
  6. ★★★★★ thread1 use time = 255
  7. ★★★★★ thread1 use time = 261
  8. ★★★★★ thread1 use time = 268
  9. ★★★★★ thread1 use time = 270
  10. ☆☆☆☆☆ thread2 use time = 321
  11. ☆☆☆☆☆ thread2 use time = 338
  12. ☆☆☆☆☆ thread2 use time = 344
  13. ☆☆☆☆☆ thread2 use time = 35
  14. -------------------------
  15. ★★★★★ thread1 use time = 121
  16. ★★★★★ thread1 use time = 232
  17. ☆☆☆☆☆ thread2 use time = 268
  18. ☆☆☆☆☆ thread2 use time = 292
  19. ★★★★★ thread1 use time = 322
  20. ☆☆☆☆☆ thread2 use time = 325
  21. ★★★★★ thread1 use time = 387
  22. ☆☆☆☆☆ thread2 use time = 390
  23. ★★★★★ thread1 use time = 395
  24. ☆☆☆☆☆ thread2 use time = 409
  25. ----------------------------------------------------
  26. ----------------------------------------------------
  27. myThread.setPriority(1);
  28. myThread2.setPriority(10);
  29. 输出:
  30. ☆☆☆☆☆ thread2 use time = 393
  31. ☆☆☆☆☆ thread2 use time = 455
  32. ☆☆☆☆☆ thread2 use time = 496
  33. ☆☆☆☆☆ thread2 use time = 509
  34. ☆☆☆☆☆ thread2 use time = 510
  35. ★★★★★ thread1 use time = 511
  36. ★★★★★ thread1 use time = 554
  37. ★★★★★ thread1 use time = 558
  38. ★★★★★ thread1 use time = 561
  39. ★★★★★ thread1 use time = 569
  40. ---------------
  41. ☆☆☆☆☆ thread2 use time = 214
  42. ☆☆☆☆☆ thread2 use time = 230
  43. ☆☆☆☆☆ thread2 use time = 237
  44. ★★★★★ thread1 use time = 257
  45. ☆☆☆☆☆ thread2 use time = 261
  46. ☆☆☆☆☆ thread2 use time = 271
  47. ★★★★★ thread1 use time = 350
  48. ★★★★★ thread1 use time = 369
  49. ★★★★★ thread1 use time = 339
  50. ★★★★★ thread1 use time = 382

随机性:运行顺序。

  1. class MyThread extends Thread {
  2. @Override
  3. public void run() {
  4. long beginTime = System.currentTimeMillis();
  5. long addResult = 0;
  6. for (int j = 0; j < 10; j++) {
  7. for (int i = 0; i < 50000; i++) {
  8. Random random = new Random();
  9. random.nextInt();
  10. addResult += i;
  11. }
  12. }
  13. long endTime = System.currentTimeMillis();
  14. System.out.println("★★★★★ thread1 use time = " + (endTime - beginTime));
  15. }
  16. }
  17. class MyThread2 extends Thread {
  18. @Override
  19. public void run() {
  20. long beginTime = System.currentTimeMillis();
  21. long addResult = 0;
  22. for (int j = 0; j < 10; j++) {
  23. for (int i = 0; i < 50000; i++) {
  24. Random random = new Random();
  25. random.nextInt();
  26. addResult += i;
  27. }
  28. }
  29. long endTime = System.currentTimeMillis();
  30. System.out.println("☆☆☆☆☆ thread2 use time = " + (endTime - beginTime));
  31. }
  32. }
  33. public class Run18_priority02 {
  34. public static void main(String[] args) {
  35. for (int i = 0; i < 5; i++) {
  36. MyThread myThread = new MyThread ();
  37. myThread .setPriority(5);
  38. myThread .start();
  39. MyThread2 myThread2 = new MyThread2 ();
  40. myThread2 .setPriority(6);
  41. myThread2 .start();
  42. }
  43. }
  44. }

结果:

  1. ☆☆☆☆☆ thread2 use time = 981
  2. ★★★★★ thread1 use time = 992
  3. ★★★★★ thread1 use time = 996
  4. ☆☆☆☆☆ thread2 use time = 1034
  5. ☆☆☆☆☆ thread2 use time = 1137
  6. ☆☆☆☆☆ thread2 use time = 1138
  7. ☆☆☆☆☆ thread2 use time = 1150
  8. ★★★★★ thread1 use time = 1182
  9. ★★★★★ thread1 use time = 1185
  10. ★★★★★ thread1 use time = 1188

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线优先级

    1、概述: 多线程的执行时抢占式执行,人为不能绝对控制线程的执行顺序,在开发的时候有些线程需要尽量的靠前执行,人为可以采用手段去影响线程执行顺序,这种手段成为是线程优先级

    相关 线优先级

    Java提供一个线程调度器来监视启动后进去就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行. 线程的优先级用数字表示,范围从1~10; Thread.MI