What happens if we call the run method right away?

I have a class called "TestRunnable" that overrides the launch method by implementing Runnable. Starting the overridden start method as follows:

TestRunnable nr = new TestRunnable();
Thread t = new Thread(nr);
t.setName("Fred");
t.start();
  • What if I call t.run();
  • What happens if we don't call t.start();?
+4
source share
2 answers

A method runis another method. If you call it directly, then it will be executed not in another thread, but in the current thread.

Here is my test TestRunnable:

class TestRunnable implements Runnable
{
   public void run()
   {
      System.out.println("TestRunnable in " + Thread.currentThread().getName());
   }
}

Output if only called start:

TestRunnable in Fred

Output if only called run:

TestRunnable in main

start , Thread . , Thread .

, : ()

+8

start, run , , . run , , run, .

0

All Articles