Saturday, 15 September 2018

Can I call a run method of thread class directly?

Yes, you can call the run method of a thread class directly. But when you call ‘run’ method directly, it executes like a normal method execution.

Application.java
package com.sample.app;

public class Application {

 private static Thread thread = new Thread() {
  public void run() {
   System.out.println("Hello World");
  }
 };

 public static void main(String args[]) {
  thread.run();
  thread.run();
 }
}

Output
Hello World
Hello World

You may like

No comments:

Post a Comment