• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt in Marcus Green's mock question on threads

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I saw this sample question in Marcus Green�s mock test1

]http://www.jchq.net/mockexams/exam1.htm#Question%2019)]



Answer provided :
4) Compilation and output of either "vandaleur", "vandaleur 0", "vandaleur 0 1" "vandaleur 0 1 2" or "vandaleur 0 1 2 3"

If that seems a vauge answer it is because you cannot be certain of the system that the underlying OS uses for allocating cycles for a Thread. The chances are that once the thread has been spun off in the call to start in the method piggy the main method will run to completion and the value of sName will still be vandeluer before the Thread modifies it. You cannot be certain of this though.


My Doubts:

1) Since the start function is not explicitly called on an object reference how come there is no compiler error?(start is an instance method of java.lang.Thread class)

2)( fine, there is no complier error)... Though the running this program does not guarantee that this thread will be run immediately... the static sName variable should at least get changed to �vandeluerwaggy� before anything else even before starting the thread� because we are calling the �piggy� method whose first statement is to update sName. How come this is not reflected in the output. I tried running the code and got an output : �vandeluer�.
How is this possible� can anyone explain�???



Thanks in advance,
Radha

[ August 21, 2007: Message edited by: Radhika ]

[ August 21, 2007: Message edited by: Radhika ]

[ August 21, 2007: Message edited by: Radhika ]
[ August 21, 2007: Message edited by: Radhika ]
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) "piggy" is a member function of class "Tux" which extends from "Thread". This class inherits all the methods from class "Thread" also. So, calling start() is just calling a member function from another member function and there is no need to specify this.start().

PS: Internally, JVM passes a reference of "this" to every member function call as a parameter.

2) The call to method "piggy" receives a parameter named "String sName", which is a local variable for this method call. This local variable causes a shadow effect and hides the static class variable. So, any changes done to this variable are actually being done to the local variable and NOT the static variable.

Hope this clarifies.

-Gaurav
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radha,

1. What you said is correct. start(); is a instance method of Thread class.
Since the Tux class extends Thread class



the instance method start() is inherited in the child class ( Tux )

Thus calling start() ( In piggy() method ), is equivalent to calling this.start()


2.

The argument list of the piggy() method has a String variable named sName. Which is a local variable of that method having the same name as of class level static varible 'sName'.

Thus any changes to variable sName in piggy() will change the value of the local variable rather than of class level static variable.

In case we want to change the value of static variable we will say,

OR



Hope it helped.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fine sName is a local variable in piggy method but what about the sName in run method. Isn't it referring to the static variable.
I am not able to understand why is the system.out in main method executed before the run method runs.
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Gaurav Bhatia and Deepak Vadgama ..that clarifies my doubt...i should confess that it was such a silly one... (at least the one about inheritance )



To neha verma, thanks for raising that doubt..it got a few things clarified for me...i would like to explain..

i tried inserting a System.out.print statement in run(),to check if the compiler actually called the run() method ...it did... and i was surprised when i got the result:




vandeleur
vandeleur 0
vandeleur 0 1
vandeleur 0 1 2
vandeleur 0 1 2 3

we have to realize here that start() method begins a new thread of execution... here the compiler just chose to continue running the main thread before running the newly created thread...

so first the result of the unchanged static(you were right)String..."vandeleur"
then the result of the changed ones..

vandeleur 0
vandeleur 0 1
vandeleur 0 1 2
vandeleur 0 1 2 3

Guess that clarifies your doubt..

Radha
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

we have to realize here that start() method begins a new thread of execution... here the compiler just chose to continue running the main thread before running the newly created thread...


But do you mean that this would be machine dependent behaviour or this is the output we will always get. How can we come to know if the execution of main thread will continue or the next thread will start and then completion of main thread. Please throw some light.
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from what i know of theads, not much is guaranteed... the order of execution of threads is JVM dependant...the compliler decides which to run
... i'm not sure if such vague questions will be asked in the exam unless they also provide a choice reflecting that...
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic