Vous êtes sur la page 1sur 6

6/13/13

about starting a new Thread (Threads forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

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

JavaRanch Java Forums Java Threads and Synchronization

Author
Arka Guhathakurta Ranch Hand Joined: Mar 01, 2009 Posts: 46

about starting a new Thread


posted 4/15/2009 11:56:40 AM

Hi, Let us consider the following example:

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 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 .

n e wR u n n a b l e ( ) { p u b l i cv o i dr u n ( ) { / / O v e r r i d i n gr u np e r f e c t l yo k S y s t e m . o u t . p r i n t l n ( " H e l l o ! " ) ; } p u b l i cs y n c h r o n i z e dv o i ds t a r t ( ) { / / T h i si sa m b i g u o u st om e S y s t e m . o u t . p r i n t l n ( " I n s i d ea n o n y m o u ss t a r t " ) ; r u n ( ) ; } } . s t a r t ( ) ; O u t p u t I n s i d ea n o n y m o u ss t a r t H e l l o !

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

Walsh graham Greenhorn Joined: Apr 10, 2009 Posts: 18

posted 4/15/2009 2:14:25 PM

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

about starting a new Thread (Threads forum at JavaRanch)


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 Joined: Apr 13, 2009 Posts: 96

posted 4/15/2009 7:57:50 PM

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 .

n e wT h r e a d( n e wR u n n a b l e ( ) { p u b l i cv o i dr u n ( ) { / / O v e r r i d i n gr u np e r f e c t l yo k S y s t e m . o u t . p r i n t l n ( " H e l l o ! " ) ; } p u b l i cs y n c h r o n i z e dv o i ds t a r t ( ) { / / T h i si sa m b i g u o u st om e S y s t e m . o u t . p r i n t l n ( " I n s i d ea n o n y m o u ss t a r t " ) ; r u n ( ) ; }) . s t a r t ( ) ;

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

posted 4/16/2009 11:40:51 AM

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 Joined: Jan 28, 2003 Posts: 3081

posted 4/16/2009 5:00:40 PM


A rka Guhathakurta wrote:

Hi, First of all thanks to all of you for replying.

www.coderanch.com/t/441100/threads/java/starting-Thread

You can check the threads created with something like this;

2/6

6/13/13

about starting a new Thread (Threads forum at JavaRanch)


You can check the threads created with something like this;

I like...

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.

A rka 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

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3081

posted 4/16/2009 5:09:46 PM

Since you had suggested your question has not been answered I will re-address the original post. (The answer has already been given though):

A rka Guhathakurta wrote:

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.

A rka 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.

www.coderanch.com/t/441100/threads/java/starting-Thread

3/6

6/13/13

about starting a new Thread (Threads forum at JavaRanch)


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

posted 4/16/2009 5:22:44 PM

Nicely put Steve

posted 4/17/2009 8:35:08 AM

Thanks steve , warret and John for resolving my query

posted 4/17/2009 9:28:33 AM

Hi, I want all of you to help me reason this code.


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 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 .

n e wT h r e a d ( n e wM a i n C l a s s ( ) , " A n o n y m o u sT h r e a d " ) { @ O v e r r i d e p u b l i cv o i dr u n ( ) { S y s t e m . o u t . p r i n t l n ( T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) ) ; } @ O v e r r i d e p u b l i cs y n c h r o n i z e dv o i ds t a r t ( ) { S y s t e m . o u t . p r i n t l n ( " I n s i d es t a r tm e t h o d " ) ; r u n ( ) ; } } . s t a r t ( ) ;

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 Joined: Jan 28, 2003 Posts: 3081

posted 4/17/2009 5:42:11 PM

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 ) .

about starting a new Thread (Threads forum at JavaRanch)

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

posted 4/17/2009 5:55:57 PM

I want all of you to help me reason this code.

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

posted 4/20/2009 9:03:16 AM

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 ( )

As per Sun site 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 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

about starting a new Thread (Threads forum at JavaRanch)

subject: about starting a new Thread

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/441100/threads/java/starting-Thread

6/6

Vous aimerez peut-être aussi