简体   繁体   中英

Java - thread doesn't start

When i try to start Thread (u) it does nothing! this is what i have inside my class:

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

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 = 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.

In the event that the target Runnable is null, Thread run() simply doesn't do anything other than exit. Thanks to Jon.

Looks like you're passing in a null Runnable. Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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