1

What is the need to run the thread through start method? Why not we call directly run method ?

What will happened if combined the code of start() and run() 
to make it as single method run()

Dont explain the difference between both method, i want to know about this scenario.

R D
  • 1,330
  • 13
  • 30
  • @tmarwen I want to know more detailed code inside start and run method – R D Jul 19 '14 at 17:30
  • How would one *start* a thread without such a method? In this case, `run` should be taken as "run this code [when the thread starts]", not "start running the thread". – user2864740 Jul 19 '14 at 17:34
  • @RajavelD You could read the source code if you just want to see the codes. They are freely available and most likely you have the source on your machine right now. – takendarkk Jul 19 '14 at 17:37
  • +1 to @ovunccetin for the only answer here that actually addresses the question (i.e., what is run() _for_, and what is start() _for_). – Solomon Slow Jul 20 '14 at 01:07
  • The code of the `start()` method is part of the standard library of Java. You cannot "combine it with the method run" because the standard implementation accesses private methods and fields that you cannot access if you copied the code of `start()` to `run()` in a subclass. It looks like you don't understand the concept and the purpose of a `Thread` in Java. Read the "Duplicate" question carefully, it's very important that you understand the concept first before asking questions about things that are not possible. – Erwin Bolwidt Jul 20 '14 at 06:43
  • IF you know the start method implementation , then obviously you don't need start method. You can put the logic in run method in that case. By the way why JVM executes public static void main()?..Why not some other like private final String myOwnMain(). If you know the JVM implementation or how to implement a JVM , then definitely we can ask the JVM to call my own method instead of routine boring main method. So Java is all about vendor implementations. You are just calling the methods that someone already implemented. – Stunner Aug 13 '14 at 11:26
  • James gosling is father of java. He wanted to call main method first. If I would have invented java, I would have renamed main method to nothingIsMain() ... – Stunner Aug 13 '14 at 11:31

5 Answers5

3

When program calls start() method a new Thread is created and code inside run() method is executed in new Thread while if you call run() method directly no new Thread is created and code inside run() will execute on current Thread.

  • Each thread starts in a separate call stack.

  • Invoking the run() method from main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.

Please refer What if we call run() method directly instead start() method? also read Difference between start and run method in Thread

rachana
  • 3,344
  • 7
  • 30
  • 49
2

You need to know that thread can have different states. Based on http://www.tutorialspoint.com/java/java_multithreading.htm there are 5 states of thread

  • new - thread was created and can be started
  • runnable - thread is executing
  • waiting - thread is waiting for other thread to finish; other thread will have to inform this thread that it finished by calling for example notify method on shared lock
  • timed waiting - similar to waiting but thread will stop waiting after some time automatically without waiting for signal from other thread
  • terminated - thread finished its task

run method contains only code which needs to be executed when thread will work (when it will be in runnable state).

start method is needed to take care of changing state of threat from new to runnable so it could start working and use its own resources (processor-time for instance) to execute code from run method.

When you call yourThread.run() code from run method will be executed by thread which invoked this method, but if you use yourThread.start() then code from run method will be executed using resources of yourThread.

Take a look at this example

public static void main(String[] args) {
    System.out.println("main started");

    Thread t = new Thread(){
        public void run() {
            try {Thread.sleep(2000);} catch (InterruptedException consumeForNow) {}
            System.out.println("hello from run");
        };
    };
    t.run();

    System.out.println("main ended");
}

Code in run method will pause thread which runs it for two seconds (because of Thread.sleep(2000);) so you can see hello from run after two seconds.

Now output looks like this

main started
hello from run
main ended

because code in run method was executed by main thread (the one handling public static void main(String[] args) method), also because of two second pause part

hello from run
main ended

was printed later.

Now if you change t.run() to t.start() code in run method will be executed by t thread. You will see it by observing result which would be

main started
main ended

(from main stream) and after two seconds

hello from run
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

run() method defines what the thread will do. start() method starts the thread to perform its task implemented by the run method.

If you call the run method directly, it is performed by the caller thread. However, the start method causes to process the task in a newly started thread. In the former case, the caller thread waits for the run method to be completed. In the later case, on the other hand, the newly created thread executed asynchronously and so the caller thread continues its job without waiting the completion of the run method.

ovunccetin
  • 8,443
  • 5
  • 42
  • 53
0

If you directly call the run() method of the thread then the code inside of that method will be run in the thread that calls the run() method. Calling start() will create a new thread and execute the code in the run() method on the new thread.

Put simply calling run() directly is probably a bug since you aren't actually creating a new thread.

See the following link for a reference.

http://javarevisited.blogspot.co.uk/2012/03/difference-between-start-and-run-method.html

Chris
  • 171
  • 1
  • 7
0

In simple term

invoke Thread.start in order to start the new thread.

If you call run method directly then its just like a normal method call in the same thread.

What JavaDoc - Thread#start() states:

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

see Defining and Starting a Thread a Java Tutorial

Read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76