线程方法调用没有Thread实例

时间:2016-01-12 12:42:33

标签: java multithreading

为什么这个类在第11行编译没有错误,以及如何解释输出的最后一行:

class Bang extends Thread {

    static Thread t1;

    public static void main(String[] args) throws Exception {
        System.out.println("main thread ID = " + Thread.currentThread().getId());
        t1 = new Thread(new Bang());
        t1.setPriority(7);
        t1.setName("T1");
        t1.start();
    }

    public void run() {
        Thread th = Thread.currentThread();
        System.out.println(th.getId() + " " + th.getName() + "        "
                + th.getPriority() + "  " + th.toString() + "\n");
        System.out.println(getId() + " " + getName() + "  " + getPriority()
                + "  " + toString());
    }
}

输出:

主线程ID = 1 9 T1 7螺纹[T1,7,主要]

8 Thread-0 5 Thread [Thread-0,5,main]

谢谢,

1 个答案:

答案 0 :(得分:0)

@Tunaki是现货。我只是添加他说的话。

完全有三个Thread对象。

  1. 主线程对象,它是一个java.lang.Thread
  2. T1线程对象,它是一个java.lang.Thread
  3. Bang Bang对象,这是一个Bang
  4. 请注意,主线程和T1线程 生成 作为单独的线程,而Bang线程 未生成 < /强>

    您在run方法中打印的内容正在线程T1上运行。

    Thread.currentThread().getId()将返回正在运行的主题 T1 的ID 9

    this.getId()将返回 Bang 对象的ID 8 。还记得,你在T1对象之前创建了Bang对象吗?这就是为什么Bang拥有较小的id 8 ,而T1的较高的threadId 9

    希望这会有所帮助。我已更新您的代码以打印有关哈希码和类详细信息的更多信息。输出应该给出清晰的图像。

    代码:

    class Bang extends Thread {
    
        static Thread t1;
    
        public static void main(String[] args) throws Exception {
            System.out.println("main thread ID = " + Thread.currentThread().getId());
            Bang bang = new Bang(); //Creating a local variable so we can print the object details.
            t1 = new Thread(bang);
            t1.setPriority(7);
            t1.setName("T1");
            threadInfo(bang);
            threadInfo(t1);
            t1.start();
        }
        public void run() {
            threadInfo(this);
        }
        public static void threadInfo(Thread argThreadObject) {
            System.out.println("===================================");
            Thread currentThreadObject = Thread.currentThread();
            printThreadInfo("currentThreadObject", currentThreadObject);
            printThreadInfo("argThreadObject", argThreadObject);
            System.out.println("===================================");
        }
        public static void printThreadInfo(String threadObjId, Thread threadObject) {
            System.out.println(
                    threadObjId + ": " 
                    + threadObject.getId() + ":"
                    + threadObject.getName() + ":" 
                    + threadObject.getPriority() + ":" 
                    + threadObject.toString() + " instanceId: "
                    + threadObject.hashCode() + "@"
                    + threadObject.getClass().getName());
        }
    }
    

    输出

    main thread ID = 1
    ===================================
    currentThreadObject: 1:main:5:Thread[main,5,main] instanceId: 1113847530@java.lang.Thread
    argThreadObject: 9:T1:7:Thread[T1,7,main] instanceId: 820485811@java.lang.Thread
    ===================================
    ===================================
    currentThreadObject: 1:main:5:Thread[main,5,main] instanceId: 1113847530@java.lang.Thread
    argThreadObject: 8:Thread-0:5:Thread[Thread-0,5,main] instanceId: 912528090@Bang
    ===================================
    ===================================
    currentThreadObject: 9:T1:7:Thread[T1,7,main] instanceId: 820485811@java.lang.Thread
    argThreadObject: 8:Thread-0:5:Thread[Thread-0,5,main] instanceId: 912528090@Bang
    ===================================