This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Thread run method. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Thread run method." Watch "Doubt in Thread run method." New topic
Author

Doubt in Thread run method.

rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Look at this code:

Output is:

0 Thread-0
1 Thread-0
2 Thread-0
3 Thread-0
4 Thread-0


In the same code if i comment the line
//tt.setPriority(10);

Output is:

0 main
0 Thread-0
1 main
1 Thread-0
2 main
2 Thread-0
3 main
3 Thread-0
4 main
4 Thread-0

see, when the setPriority(10) method is commented the tt.run(); calls the run method and gives a ouput like this.
But when the setPriority(10) is included the tt.run(); is never executed.
I cant understand the behaviour of this code
could anyone explain this.
Thanx.
[ November 06, 2004: Message edited by: Barry Gaunt ]
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Hope this helpsThis one..


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

see, when the setPriority(10) method is commented the tt.run(); calls the run method and gives a ouput like this.
But when the setPriority(10) is included the tt.run(); is never executed.
I cant understand the behaviour of this code
could anyone explain this.
Thanx.


The tt.run() method is *NOT* the run() method that you defined. It is the default run() method of the Thread class. Notice that you are not overriding the Thread class here.

Just because tt.run() calls tc.run() under certain conditions is pure dumb luck as it was never meant to be called directly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Henry
I dont understand both of your sentences.
quote 1:
___________________________________________________________________________
The tt.run() method is *NOT* the run() method that you defined. It is the default run() method of the Thread class.
___________________________________________________________________________

If tt.run() method is not the one i defined and if it is going to be the default run() method of the Thread class ,then what about the output i get when i run the code.

quote 2:
___________________________________________________________________________
Just because tt.run() calls tc.run() under certain conditions is pure dumb luck as it was never meant to be called directly.
___________________________________________________________________________
calling the run method directly is legal but it will not run in a seperate thread rather it goes in to the current call stack and the run method will be executed like any other normal method.
But when i redifine the class to extend Thread and i call both start() and run() methods the output is same regardless of whether the setpriority() method is commented or not.Only if i implement the runnable interface the output is different with comments and without comments.
Thanks.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132


What is happening here is:
When start is called, actual thread is started (Thread-0).
When run method is called, main thread is started.

This can be proved as :
Run this code by uncommenting line no.1,2,3.
Run this code by uncommenting line no.1,2,3,4,5,6
Run this code by uncommenting line no.1,2,3,4,5,6,7,8,9
Run this code by uncommenting line no.7,8,9
Compare results.
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Thanks nitin

But you try this you remove all the comments in your code run the code see the result.
Comment only the line tt.setPriority(10) run the code and see the result.
Why is the difference in output?
Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

If tt.run() method is not the one i defined and if it is going to be the default run() method of the Thread class ,then what about the output i get when i run the code.


The run() method you defined is part of the THcheck class which does not extend the thread class. What is happening is that the thread class run() method is calling your run() method. And the reason it is calling it is because it thinks it is being called internally.

calling the run method directly is legal but it will not run in a seperate thread rather it goes in to the current call stack and the run method will be executed like any other normal method.
But when i redifine the class to extend Thread and i call both start() and run() methods the output is same regardless of whether the setpriority() method is commented or not.Only if i implement the runnable interface the output is different with comments and without comments.
Thanks.


The run() method is being called, but again, it is *not* your run() method when you use the interface. It is your run() method when you extend thread. Whether the default thread run() method, calls your run() method is not defined for this case.

Henry
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
[offtopic]Thanks for dropping in to the SCJP Forum Henry, it's an honour to have you with us. I like your new edition of Java Threads. It's going to be a great help in getting to know all the new Java 1.5 features.
[/offtopic ]

Rengarajan, why are you calling the run method anyway? Normally we create a thread (possibly loading it up with a Runnable), set its priority and/or daemon status, and then fire it off by calling the start() method, and possibly joining it later.

Calling the run method is a NONO and selecting such an option in an SCJP would be an incorrect choice.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Barry
You are right you call the start method to start a thread.But what surprised me is the difference in the output.When i dont set a priority for the thread and invoke the run method the run method is invoked and runs like any other normal method.But when i set the priority and call the run method it is not invoked.I just want to know why the code bahaves differently when the thread priority is set.
Thanks.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This is a pathological example. You have two unsynchronized threads accessing the System.out I/O stream at the same time, so the behavior is going to be rather unpredictable at the least. Trying to control the behaviour by setting the priority is futile, as is trying to understand the behaviour. You are not using threads as they are meant to be used so spending effort to explain this behaviour is not worthwhile (especially because it's easier to use threads the correct way.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Originally posted by rengarajan vaikuntam:
Barry
You are right you call the start method to start a thread.But what surprised me is the difference in the output.When i dont set a priority for the thread and invoke the run method the run method is invoked and runs like any other normal method.But when i set the priority and call the run method it is not invoked.I just want to know why the code bahaves differently when the thread priority is set.
Thanks.


I seem to enjoy the punishment, so I'll try again...

Since you are not supposed to call the thread class run() method, whether the run() method calls your run() method, when it calls, or anything else it does is not specified... However, if I have to take a guess...

I speculate that when you call the default run() method, it believes it is being called internally, and eventually calls your run() method. However, when you change the priority, it allows the first, and correct run() method to finish, this changes some of the state in the thread object. So later when you call the thread's run() method, it doesn't call your run() method.

Again, since this is all not defined, this is PURE speculation. Unfortunately, if you really need to know more, you'll have to load up your favorite IDE and hack through it yourself...

[offtopic] Barry, I am glad you liked the new edition. It is nearly a total rewrite, that addresses every issue (i.e. complaint) that we received and gathered over the last few years. Enjoy!
[/offtopic]

Henry
rengarajan vaikuntam
Ranch Hand

Joined: Oct 04, 2004
Posts: 37
Hi Barry

quote:
___________________________________________________________________________
You are not using threads as they are meant to be used so spending effort to explain this behaviour is not worthwhile (especially because it's easier to use threads the correct way.)
___________________________________________________________________________
Lets take a look at one of the questions from Marcus Greens mock exam.

31)What will happen when you attempt to compile and run the following code?

1) Compile time error
2) Output of first0, second0, first0, second1
3) Output of first0, first1, second0, second1
4) Runtime error

The Ans is 3)Output of first0, first1, second0, second1

In this question he has subclassed a thread class,invoked the start() method,made the Thread to sleep but instead of overriding the run() method he has overridden the start() method.So this actually is not a Thread.Here Threads are not used the right way.But it is not surprising to get such questions in the exam.You tend to get stuck here.



(Code formatted)
[ November 08, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The difference between these two questions is that the first is a non-deterministic situation and the second is deterministic. In the first, threads have been started and are running independently of one another. The second, as you say, no threads have been started and every thing is running in the main thread.
My approach to these questions is to first determine whether threads are being used correctly. If there turns out to be a misuse but a single thread of execution then then answer is usually clear. If there is a misuse and multiple threads running then don't waste time on doing a detailed analysis, if there is an option with "don't know", "undeterminable", choose that. On SCJP 1.4, IMHO, that will do the trick.

In real life, I would concentrate on fixing the misuse of threads rather than analysing behavour of obviously misused threads. That is, an independent code review is better than some allnightlong debugging session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Thread run method.
 
Similar Threads
After run() method is finished it can not be restarted again while thread reference exist into .Why?
Doubt in Threads Synchronization
Thread question......
could you pls explain this code and the output.
Which method??