When would you call java's thread.run() instead of thread.start()?
You might want to call run() in a particular unit test that is concerned strictly with functionality and not with concurrency.
What will happen use run() instead of start() of a thread?
Calling run
directly on a Thread
object defeats the point of having the Thread
in the first place.
If you call run
, then run
will execute in the current Thread
, as a normal method. You must call the start
method on the Thread
to have run
execute in a different Thread
.
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Why do we need to call thread.start() instead of thread.run()?
The MyThread
class is not a thread. It is an ordinary class that implements Runnable
and has a method called run
.
If you call the run
method directly it will run the code on the current thread, not on a new thread.
To start a new thread you create a new instead of the Thread
class, give it an object that implements Runnable
, and then call the start
method on the thread object. When the thread starts, it will call the run
method on your object for you.
Another way to start a thread is to subclass Thread
and override its run
method. Again to start it you must instantiate it and call the start
method, not the run
method.The reason is the same: if you call run
directly it will run the method in the current thread.
See Defining and Starting a Thread for more information about starting new threads in Java.
run() is never called by Thread.start() method
run() is never called by Thread.start() method
You code actually works on my system but that it doesn't work on your's, demonstrates that you have a classic race condition.
Inside of main()
, the NewThread
is constructed but the Java language says that it can reorder operations so that the operations in a constructor can happen after the constructor finishes. So it is possible that main()
might finish before the NewThread
has actually been started which can result in the JVM shutting down without running the thread.
Because of instruction reordering, you should never have a thread auto-start itself inside of the constructor. See: Why not to start a thread in the constructor? How to terminate?
You should instead do:
public NewThread() {
t = new Thread(this, "Thread created by Thread Class.");
System.out.println("Created by constuctor:" + t);
// don't start here
}
public void start() {
// start in another method
t.start();
}
public void run() {
System.out.println("run() method called.");
}
...
public static void main(String[] args) {
NewThread nt = new NewThread();
nt.start();
}
Since the NewThread
has the same daemon status as your main thread (which is non-daemon) the JVM will not shutdown until nt.run()
completes.
Java multithreading difference between run() and start()
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.
Difference between running and starting a thread
Thread.run()
does not spawn a new thread whereas Thread.start()
does, i.e Thread.run
actually runs on the same thread as that of the caller whereas Thread.start()
creates a new thread on which the task is run.
What is the difference between Thread.start() and Thread.run()?
No, you can't. Calling run will execute run()
method in the same thread, without starting new thread.
Related Topics
How to Convert a String to a Date Using Simpledateformat
How to Handle Pop-Up in Selenium Webdriver Using Java
What Causes a Java.Lang.Stackoverflowerror
How to Run Linux Commands in Java
What Makes Reference Comparison (==) Work for Some Strings in Java
Why Does the Ternary Operator Unexpectedly Cast Integers
How to Add a Resources Folder to My Java Project in Eclipse
List Versus Arraylist as Reference Type
How to Populate a Listview in Javafx Using Custom Objects
Accessing a Variable from Another Class
Can Java Call Parent Overridden Method in Other Objects But Not Subtype
Setting Default Values for Columns in JPA
Differencebetween a Javabean and a Pojo
Spring Boot Rest Service Exception Handling
When Would You Use a Weakhashmap or a Weakreference