• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

q on Thread-mock exam

 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5. What is the Thread output?

A) Executing the myTest() method
Executing run() method
B) Executing run() method
Executing the myTest() method
C) No output
D) Compile time error
The answer they give is A)
Could some explain why and, more important if that anytime the
output is A)?
I ran the code with an IDE with Debug and the output was anytime B)
how this happens?
thanks in advance
rgds,
cristi

 
deep_scjp
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i Surprise how answer is A)
becouse we are not creating object of class T in main method so without and Object how class T' run and and myTest method will execute
according to me first main thread should start then other thread's
can any one explain this

 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer should be A or B. This will be dependent on the system and the schedular. What happens is you are creating another thread t and having that thread start running. So you have two threads running. On my machine, the schedular doesn't give control over to the new thread but lets the main thread still run, so I get the answer A, but as you pointed out, on your machine, the schedular does give control over to thread t, so you get answer B.
So I think the real answer is A or B, or more accuratley, this is system dependent and there is no way of knowing which thread will run first.
Bill
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cristi, deep ...
This question is very misleading; the answer depends on the underlying JVM scheduler; you may see either A or B as a result.
When I compiled and ran the code using JDK 1.3 on a Win98 system the output was A; probably because the <code>main()</code> thread was scheduled ahead of the thread handling <code>t.start()</code>.
If I set the priority of 't' to MAX_PRIORITY, then I see the result given by B.
Hope that helps.
Jane
 
Cristi Tudose
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First,sorry for post this topic in 2 forums
I was inattentive.
As you say Jane deep...
But could we meet in real exam such questions???
And in such case,what answers should we checked?
best rgds to all,
cristi

 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cristi,
The real exam questions are clearly worded as are the choices.
You won't see anything that relies on a specific JVM implementation.
Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic