Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread run method.

 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope this helpsThis one..
 
Henry Wong
author
Marshal
Pie
Posts: 21382
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21382
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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.
 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21382
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic