-3

My JavaApplication class extends the Thread class, but does not having run method. If I run the below code how does start method works and behaves?

public class JavaApplication1 extends Thread {
    // public void run(){}

    public static void main(String[] args) {
        JavaApplication ja = new JavaApplication();
        ja.start();
    }
}

Could anyone give some advise on this?

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • Did you try executing the code? What happens? – BackSlash Sep 25 '17 at 18:04
  • @BackSlash It is Possible Duplicate ? – Ravi Sep 25 '17 at 18:05
  • 5
    The answer is in the javadoc: https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#run--. *If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.*. – JB Nizet Sep 25 '17 at 18:06

3 Answers3

1

It will run the run() method in Thread which will do nothing and end.

From JDK 8

/**
 * If this thread was constructed using a separate
 * <code>Runnable</code> run object, then that
 * <code>Runnable</code> object's <code>run</code> method is called;
 * otherwise, this method does nothing and returns.
 * <p>
 * Subclasses of <code>Thread</code> should override this method.
 *
 * @see     #start()
 * @see     #stop()
 * @see     #Thread(ThreadGroup, Runnable, String)
 */
@Override
public void run() {
    if (target != null) {
        target.run();
    }
}
JustinKSU
  • 4,875
  • 2
  • 29
  • 51
1

By default, run() method in the Thread class:

/* What will be run. */
private Runnable target;

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

So, if you will not override run() method - nothing will not happen, target will be null

jam007
  • 21
  • 2
0

Nothing special. Thread class contains default implementation of run() method. By default it do not anything.

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35