Thread.start() method (native method) of Thread class actually does the job of running the Thread.run() method in a thread. If we directly call Thread.run() method it will executed in same thread, so does not solve the purpose of creating a new thread.
In Java, the Thread.start()
and Thread.run()
methods are related to the execution of threads, but they serve different purposes.
- Thread.start():
- Invoking the
start()
method on aThread
object is used to begin the execution of a new thread of control. - When
start()
is called, it internally calls therun()
method on the new thread. - The actual work of the thread should be defined in the
run()
method.
Example:
javaThread myThread = new MyThread();
myThread.start();
- Invoking the
- Thread.run():
- The
run()
method is the entry point for the code that will be executed in the new thread. - If you call the
run()
method directly, it will be executed in the context of the current thread, not as a separate thread. - It’s the method where you put the code that you want to be executed in the new thread when
start()
is called.
Example:
javaThread myThread = new MyThread();
myThread.run(); // This will not create a new thread
- The
In summary, start()
is used to initiate the execution of a new thread, and it internally calls the run()
method of that thread. Calling run()
directly will not create a new thread; it will execute the code in the context of the current thread. Typically, you should use start()
to launch a new thread and let it execute its run()
method independently.