开发者社区> 问答> 正文

Java 实例 - 查看线程优先级

Java 实例 - 查看线程优先级

展开
收起
问问小秘 2020-02-13 18:00:20 1268 0
1 条回答
写回答
取消 提交回答
  • 以下实例演示了如何使用 getThreadId() 方法获取线程id:

    Main.java 文件 public class Main extends Object { private static Runnable makeRunnable() { Runnable r = new Runnable() { public void run() { for (int i = 0; i < 5; i++) { Thread t = Thread.currentThread(); System.out.println("in run() - priority=" + t.getPriority()+ ", name=" + t.getName()); try { Thread.sleep(2000); } catch (InterruptedException x) { } } } }; return r; } public static void main(String[] args) { System.out.println("in main() - Thread.currentThread().getPriority()=" + Thread.currentThread().getPriority()); System.out.println("in main() - Thread.currentThread().getName()="+ Thread.currentThread().getName()); Thread threadA = new Thread(makeRunnable(), "threadA"); threadA.start(); try { Thread.sleep(3000); } catch (InterruptedException x) { } System.out.println("in main() - threadA.getPriority()="+ threadA.getPriority()); } } 以上代码运行输出结果为:

    in main() - Thread.currentThread().getPriority()=5
    in main() - Thread.currentThread().getName()=main
    in run() - priority=5, name=threadA
    in run() - priority=5, name=threadA
    in main() - threadA.getPriority()=5
    in run() - priority=5, name=threadA
    in run() - priority=5, name=threadA
    in run() - priority=5, name=threadA
    
    2020-02-13 18:00:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载