美文网首页
Java 查看线程优先级

Java 查看线程优先级

作者: 西贝巴巴 | 来源:发表于2021-03-17 08:47 被阅读0次
package com.company;

/*
使用 getThreadId() 方法获取线程id
*/
public class GetThreadIdTest extends Object {

    private static Runnable makeRunnable() {
        Runnable r = new Runnable() {
            @Override
            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());
    }
}

相关文章

网友评论

      本文标题:Java 查看线程优先级

      本文链接:https://www.haomeiwen.com/subject/ctlcqltx.html