• Post Reply Bookmark Topic Watch Topic
  • New Topic

about starting a new Thread  RSS feed

 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let us consider the following example:


If the above code is written inside main method, then will it start a new thread? The code would have started a new thread had there been only run method but this anonymous class contains a start method as well which is not inherited from Runnable( it contains only run method). Thus the start method is a new one not overriden. So I believe this simply calls the run method but does not start a new Thread.
 
Walsh graham
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hi,

nearly, but not quite. Remember we create Thread objects in various ways, one of them is via an implementation of the Runnable interface. Once created we call start() on the *Thread* object, not an implementation of the Runnable interface. If you remove the "start()" method from your code, the compiler will rightly complain.

You can check the threads created with something like this;

System.out.println("Thread: " + Thread.currentThread().getName());

hope this helps
 
John Kimball
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All your code does is create an anonymous class and executes it in the primary thread.

If you did something like this, it would be a different story:


What you should see is "Hello!" (and only "Hello!", not "Inside anonymous start").

Remember: The Runnable interface doesn't really mean much without an accompanying Thread instance.
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
First of all thanks to all of you for replying.
You can check the threads created with something like this;

System.out.println("Thread: " + Thread.currentThread().getName());

Thread.currentThread().getName().. fetches the name of the current thread but however this class is anonymous so this function will probabaly return null or it will return main in case the above doesn't start a thread.

What you should see is "Hello!" (and only "Hello!", not "Inside anonymous start").

If the output displays "Hello" then it means run method is executing and start has already executed . Thus "Hello" will display after printing Inside anonymous start. I don't find any reason for not printing the aforesaid statement. My question is still unanswered.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arka Guhathakurta wrote:Hi,
First of all thanks to all of you for replying.
You can check the threads created with something like this;

System.out.println("Thread: " + Thread.currentThread().getName());

Thread.currentThread().getName().. fetches the name of the current thread but however this class is anonymous so this function will probabaly return null or it will return main in case the above doesn't start a thread.


A Thread will always have a name, it will not be null. You should try the code out to see how it works. The point of the code was to demonstrate what thread your code will execute in.

Arka Guhathakurta wrote:
What you should see is "Hello!" (and only "Hello!", not "Inside anonymous start").

If the output displays "Hello" then it means run method is executing and start has already executed . Thus "Hello" will display after printing Inside anonymous start. I don't find any reason for not printing the aforesaid statement. My question is still unanswered.


No. Again, you should run the code to see how it works (try adding the Thread.currentThread().getName() to the run() method just to see what happens as well). Look at John's code carefully. It does not call the start method of the anonymous Runnable instance. It calls start() on the Thread instance - which in turn calls Runnable's run() method (from inside a new thread). The start() method you define in Runnable serves no purpose when a Runnable is used properly.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you had suggested your question has not been answered I will re-address the original post. (The answer has already been given though):

Arka Guhathakurta wrote:
If the above code is written inside main method, then will it start a new thread?


No. It executes inside the main thread. You could have seen this had you tested the Thread.currentThread().getName() code. John has already said this, and Warrent gave you the reason and a tool needed to see for yourself.

Arka Guhathakurta wrote:
The code would have started a new thread had there been only run method


Incorrect. The code would have started a new thread if the Runnable was passed into a Thread and the Thread was started. But nothing in your code creates a new Thread.

but this anonymous class contains a start method as well which is not inherited from Runnable( it contains only run method). Thus the start method is a new one not overriden. So I believe this simply calls the run method but does not start a new Thread.


Your conclusion is correct: your code does not generate a new Thread. The reasoning is wrong. It isn't because you defined a start() method in the Runnable interface. It is because you never created an instance of Thread and started it.
 
Walsh graham
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicely put Steve
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks steve , warret and John for resolving my query

 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want all of you to help me reason this code.

I have put the above code inside main method and compiled. MainClass is the public class containing main method implements Runnable and has it's own run method. But here I am overriding the run method of Thread class. The above code does not start a new Thread but outputs the print statements in correct order. If i remove the start method the code rightly compiles a thread named Anonymous thread.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow. You are allowed to override the start() method in Thread? The start() method should be final if you ask me.

So your question is: If you override the start() method in Thread, then the code isn't run in a new thread. Look at the API for java.lang.Thread#start():


If you can't override the method and still provide the contract then you shouldn't override the method. In your code you override the start() method but don't make it begin a new native thread. Your overriden start() method, then, doesn't meet the contract defined for what Thread#start() does, you broke the contract, and so it should not be a surprise that the code doesn't do what you expect.
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want all of you to help me reason this code.


As you already figured out, it is the thread's start() method that creates a thread and have that thread call the run method. If you override the start() method to call the run() method directly, then it won't create a new thread.

The other issue, that you may not be aware of. It is the thread's run() method that calls the run() method of the runnable instance passed in the constructor. If you both pass a runnable class and override the run() method, then of course, it is the overriden run() method that runs. This is why the mainclass' run() method is never called.

Henry
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks to everyone who posted and helped me out in solving my query. Now I have a clear concept about starting Threads. However I would like to point out the following points...
I am using NetBeans IDE 6.1 to write Java programs. Sometimes i also run programs without IDE(i.e. manually using java and javac). I am unaware if NetBeans is using some other class Hierarchy...
As per NetBeans this is the definition....

As per Sun site this is the definition....

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!