2

I confuse between thread.run() && thread.start() methods because

thread.start() --> start new thread so name of thread is changed (obviously)

thread.run() --> does not start new thread (run on main thread) though according to below program thread's name is changed.

Why god why?

public class DemoClass {
    public static void main(String[] z) {
        Thread t = Thread.currentThread();
        System.out.println("Main thread : " + t.getName());

        MyThread thread = new MyThread();
        thread.run();
        thread.start();
    }
}

public class MyThread extends Thread {
    public void run() {
        System.out.println("Important job running in MyThread");
        System.out.println("run : " + this.getName());

    }       
}

O/p :

Main thread : main
Important job running in MyThread
run : Thread-0
Important job running in MyThread
run : Thread-0

Please correct me if i am wrong to some concept.

exception
  • 321
  • 2
  • 4
  • 10

8 Answers8

2

There is no thread.run(). If you start the run method by calling run() method explicitly it won't act as thread(ie in parallel execution).it just a normal method call.If thread.start() internally it will call run() method and starts parallel execution.

Junaid
  • 2,572
  • 6
  • 41
  • 77
Shriram
  • 4,343
  • 8
  • 37
  • 64
  • Exactly @Shriram, but look both thread names are same so i am confused. – exception Nov 25 '14 at 06:20
  • It does not make any difference. – exception Nov 25 '14 at 06:24
  • 1
    Of course there is a Thread.run(), and here is its javadoc: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#run%28%29 Thread.run() is the first method that the new thread calls after it has been started. If anybody else writes code that calls it, that's probably a sign that they don't understand what they're doing. – Solomon Slow Nov 25 '14 at 14:14
2

From the horse's mouth:

public void 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

public void start()

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

The run method is part of the Runnable interface, which doesn't entail usage of a separate thread for execution. A call to a Runnable.run method will be executed in the current execution context.

start however is the method to call on a thread to actually launch it, and have the code in its run method to be executed asynchronously from the calling thread.

For example, you could definitely override the run method of a Thread instance, and pass this instance for execution to yet another Thread instance as if it were just a simple Runnable.

As you can see from the documentation cited at the beginning, the run method on the Thread class is provided for convenience: it let the jvm easily access the Runnable part of a thread without having to expose the Runnable object directly, but it also provides the programmer a way to embed the Runnable part in the thread directly, bypassing the need of a separate object.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52
1

It's amazing how many people can answer a question by repeating what the OP already wrote without actually answering the question.

The solution is that you are first asking for the name of the running thread, but the second time you ask for the name of the thread insurance where the code or executed. If you change the later to Thread.currentThread instead of this you will get the expected answer.

Voo
  • 29,040
  • 11
  • 82
  • 156
0

When we calls start() method every time a new Thread is created and logic inside run() method is executed with newly created Thread on other hand if application calls run() method directly no new Thread is created and logic inside run() will execute on currently executing Thread.

Also you can not call start() method twice on thread object. once started, second call of start() will throw IllegalStateException in Java while you can call run() method twice.

Sai prateek
  • 11,842
  • 9
  • 51
  • 66
  • What is issue? You are printing after stating of new thread. Have you tried to do the same by creating two different thread explicitly? – Sai prateek Nov 25 '14 at 06:40
  • Issue : thread.run() method is called by main thread not by thread 0 but output seems something incorrect. – exception Nov 25 '14 at 06:46
0

Why god why?

The Java developers had no idea how much confusion they would create when they named the main entry point for a Thread "run".

When you pass an explicit Runnable r in a call to new Thread(r), The Runnable object becomes the thread's delegate. Delegation was not as widely used back when Java was new. I bet if they could start over from scratch, there would be no public Thread.run() to confuse newbies, and delegation would be the only way to specify how a new Thread would behave.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
0

You are calling thread.getName(), Its obvious to be executed on the thread object. Instead you should try something like Thread.currentThread.getName(); This would give you the name of thread currently running.

Anand Vaidya
  • 1,374
  • 11
  • 26
-1

http://www.vogella.com/tutorials/JavaConcurrency/article.html

You definitely need to use executor pools!

Real nice article.

kaasdude
  • 1,336
  • 2
  • 8
  • 13
  • You can say that when you see an application that _continually_ creates new threads to perform bounded tasks, and then lets the threads die when the tasks are done. Thread creation is expensive. It's much better to perform tasks in re-usable "worker threads". But, performing bounded tasks is not the only reason why applications create threads. If an application creates a thread on startup that lasts the whole life of the process (e.g., a thread that waits for incoming network connections), then there is no benefit to having that thread be a member of a pool. – Solomon Slow Nov 25 '14 at 14:30
-1

start()- Only schedules the thread for executing but not actually begins the execution. run() -The actual execution is stated when the JVM calls the run method.