Академический Документы
Профессиональный Документы
Культура Документы
Java FAQ
Recent Topics
Register / Login
A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal
Author
Arka Guhathakurta Ranch Hand Joined: Mar 01, 2009 Posts: 46
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 .
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.
regards, Arka
Hi Hi, nearly, but not quite. Remember we create Thread objects in various ways, one of them is via an
www.coderanch.com/t/441100/threads/java/starting-Thread
1/6
6/13/13
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:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .
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 Joined: Mar 01, 2009 Posts: 46
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.
www.coderanch.com/t/441100/threads/java/starting-Thread
You can check the threads created with something like this;
2/6
6/13/13
I like...
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.
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
Since you had suggested your question has not been answered I will re-address the original post. (The answer has already been given though):
5
If the above code is written inside main method, then will it start a new thread?
I like...
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.
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.
www.coderanch.com/t/441100/threads/java/starting-Thread
3/6
6/13/13
Walsh graham Greenhorn Joined: Apr 10, 2009 Posts: 18 Arka Guhathakurta Ranch Hand Joined: Mar 01, 2009 Posts: 46 Arka Guhathakurta Ranch Hand Joined: Mar 01, 2009 Posts: 46
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 .
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.
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():
view plain c opy to c lipboard print ?
5
I like...
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .
s t a r t p u b l i cv o i ds t a r t ( ) C a u s e st h i st h r e a dt ob e g i ne x e c u t i o n ;t h eJ a v aV i r t u a lM a c h i n ec a l l st h er u n m e t h o d o ft h i st h r e a d .
T h er e s u l ti st h a tt w ot h r e a d sa r er u n n i n gc o n c u r r e n t l y :t h ec u r r e n tt h r e a d( w h i c h 0 9 . r e t u r n sf r o mt h ec a l lt ot h es t a r tm e t h o d )a n dt h eo t h e rt h r e a d( w h i c he x e c u t e si t sr u n 1 0 . m e t h o d ) . www.coderanch.com/t/441100/threads/java/starting-Thread
4/6
6/13/13
1 0 . m e t h o d ) .
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 Joined: Sep 28, 2004 Posts: 16787
19
I like...
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
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor) Arka Guhathakurta Ranch Hand Joined: Mar 01, 2009 Posts: 46
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....
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 .
p u b l i cs y n c h r o n i z e dv o i ds t a r t ( )
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 .
p u b l i cv o i ds t a r t ( )
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
www.coderanch.com/t/441100/threads/java/starting-Thread
5/6
6/13/13
Similar Threads Anonymous class concept how to start a thread?? anonymous inner class output Anonymous Inner class Doubt in threading
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 03:29:47 .
www.coderanch.com/t/441100/threads/java/starting-Thread
6/6