我是java新手。有人可以帮助我为什么它不调用 Run 方法。 提前致谢。
package com.blt;
public class ThreadExample implements Runnable {
public static void main(String args[])
{
System.out.println("A");
Thread T=new Thread();
System.out.println("B");
T.setName("Hello");
System.out.println("C");
T.start();
System.out.println("D");
}
public void run()
{
System.out.println("Inside run");
}
}
最佳答案
您需要将 ThreadExample
的实例传递给 Thread
构造函数,以告诉新线程要运行什么:
Thread t = new Thread(new ThreadExample());
t.start();
(不幸的是,Thread
类在各种方面的设计都很糟糕。如果它没有 run() 会更有帮助
方法本身,但确实强制您将 Runnable
传递给构造函数。然后您会在编译时发现问题。)
关于java - 线程 : Not calling run method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808895/