zoukankan      html  css  js  c++  java
  • java_多线程_优先级

    java多线程示例展示优先级源代码:

     1 package control;
     2 
     3 /**
     4  * 功能:龟兔赛跑
     5  *            线程控制优先级别
     6  * 
     7  *         总结:怎么设置优先级?
     8  *             setPriority 
     9  *             通过设置优先级,数字越大的优先级别越高(1-10)  获取cpu概率大
    10  *             默认优先级是5
    11  *             final static 
    12  *             MIN_PRIORITY  1
    13  *             MAX_PRIORITY  10
    14  *             通过写到run方法的设置优先级 还有启动线程设置优先级  在run方法中设置的生效
    15  * @author superdrew
    16  *
    17  */
    18 public class TestThread1 {
    19     public static void main(String[] args) {
    20         TortoiseThread tt = new TortoiseThread("乌龟线程");
    21         //设置线程优先级
    22         tt.setPriority(2);
    23         tt.start();
    24         
    25         RabbitRunnable rr = new RabbitRunnable();
    26         Thread tr = new Thread(rr);
    27         tr.setName("兔子线程");
    28         //设置线程优先级
    29         tr.setPriority(9);
    30         tr.start();
    31         
    32         
    33     }
    34 }
    Test.java

    乌龟线程:TortoiseThread.java

     1 package control;
     2 
     3 import java.util.Scanner;
     4 
     5 public class TortoiseThread extends Thread{
     6     private String name;
     7     public TortoiseThread(){
     8     }
     9     public TortoiseThread(String name){
    10         super(name);
    11     }
    12     int count = 0;
    13     public void run() {
    14         setPriority(10);
    15         while(true){
    16             count++;
    17             Scanner sc = new Scanner(System.in);
    18             System.out.println("乌龟领先了====加油。。。"+getName() +" "+getPriority()+" count:"+count);
    19         }
    20     }
    21 }
    TortoiseThread.java

    兔子线程:RabbitThread.java

     1 package control;
     2 
     3 /**
     4  * 兔子的线程
     5  * @author superdrew
     6  */
     7 public class RabbitRunnable implements Runnable{
     8     int count = 0;
     9     public void run() {
    10         Thread.currentThread().setPriority(5);
    11         while(true){
    12         count++;    
    13             System.out.println("兔子领先了-----快加油!"+
    14         Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()+" count:"+count);
    15         }
    16     }    
    17 }
    RabbitThread.java

    结果展示:

  • 相关阅读:
    POJ 3630 Phone List | Trie 树
    POJ 3974 Palindrome | 马拉车模板
    POJ 3422 Kaka's Matrix Travels | 最小费用最大流
    POJ 2195 Going Home | 带权二分图匹配
    POJ 3068 "Shortest" pair of paths | 最小费用最大流
    POJ 3686 The Windy's | 最小费用最大流
    洛谷 最小费用最大流 模板 P3381
    POJ 2987 Firing | 最大权闭合团
    POJ 3469 Dual Core CPU | 最小割
    POJ 3281 Dining | 最大流
  • 原文地址:https://www.cnblogs.com/superdrew/p/8086672.html
Copyright © 2011-2022 走看看