package threadwork;
public class WorkingWithThreads implements Runnable {
public static void main(String[] args) {
WorkingWithThreads wwt = new WorkingWithThreads();
}
public WorkingWithThreads() {
System.out.println("Creating Thread");
Thread t = new Thread();
System.out.println("Starting Thread");
t.start();
}
@Override
public void run() {
System.out.println("Thread Running");
for (int i = 0; i < 5; i++) {
System.out.println("Thread:" + i);
try {
Thread.sleep(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
When I run this code, it Prints Creating Thread and Starting Thread. But doesn’t prints Thread Running, that means run function is not at all called. Why is it so?
You have to call
start()on the thread to get it to start; e.g.If you were extending
Thread, you would create a new thread and callstart()on it like this:Since you are not extending
Thread, but your class implementsRunnable: