简体   繁体   English

Java-线程无法启动

[英]Java - thread doesn't start

When i try to start Thread (u) it does nothing! 当我尝试启动线程(u)时,它什么也不做! this is what i have inside my class: 这是我班上的东西:

private Updater uc;
Thread t1 = new Thread(uc);
-bunch of other code-
t1.start();

Updater.java: Updater.java:

public class Updater implements Runnable{
public void run(){
  System.out.println("I work!");
  }
}

Output is nothing. 输出什么都没有。 Anyone has idea why? 有人知道为什么吗?

Unless I'm missing something - you never initialize uc: 除非我缺少任何东西,否则您永远不会初始化uc:

uc = new Updater();

When you pass null in as the Runnable, then Thread just passes the null value through to an internal init method, which gets called from all of the Thread constructors, including the ones which take no Runnable argument. 当您将null传递为Runnable时,Thread只是将null值传递给内部init方法,该方法会从所有Thread构造函数(包括不带Runnable参数的构造函数)中调用。

In the event that the target Runnable is null, Thread run() simply doesn't do anything other than exit. 如果目标Runnable为null,则线程run()只会执行退出操作。 Thanks to Jon. 感谢乔恩。

Looks like you're passing in a null Runnable. 看起来您正在传递一个空的Runnable。 Try: 尝试:

private Updater uc = new Updater();
Thread t1 = new Thread(uc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM