This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question on threads" Watch "question on threads" New topic
Author

question on threads

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
In which of the following cases a thread will definitely be alive but not be running?


Select 3 correct options
a The thread has issued a call to wait( ).


b The thread is trying to enter a synchronized block and the monitor is not free.

c A higher priority thread comes in ready to run state.

d The thread is sleeping as a result of a call to the sleep( ) method.

e The thread calls yield().

my answers were
a, b, c (only 3 possible). But the current answer is a, b ,d.
A is ok because the thread is moved to the wait state, so it still alive.
B. according to KnB, is moved to the same state as A, but it's still alive (it's blocked).
C. I think C is correct because it is in the runnable state (not running), but it's still alive and not running.
But option D. Hmmm.. sleep() will sleep at least for a particular time , taking its lock with it, but it'll go to the same state as A and B..
anyways.. It says 3 possible answers but I see 4 possibles, unless I'm missing something.
[ June 30, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Andres Gonzalez:
In which of the following cases a thread will definitely be alive but not be running?

Select 3 correct options
a The thread has issued a call to wait( ).


b The thread is trying to enter a synchronized block and the monitor is not free.

c A higher priority thread comes in ready to run state.

d The thread is sleeping as a result of a call to the sleep( ) method.

e The thread calls yield().

..the current answer is a, b ,d.
[ June 30, 2003: Message edited by: Andres Gonzalez ]

I'm always get stumped by these type of questions because I am not sure if they are asking before or after the fact:
a)The thread has issued a call to wait( ).
Before the call, this thread is alive and running. But immediately after, it is still alive but NOT running
b)The thread is trying to enter a synchronized block and the monitor is not free.
At this moment, this thread is alive and NOT running
c)A higher priority thread comes in ready to run state.
Before it changes to a run state, this thread is alive but NOT running. Afterwards, it is definitely alive and running.
d)The thread is sleeping as a result of a call to the sleep( ) method.
Same as (a). Before the call, this thread is alive and running. But immediately after, it is still alive but NOT running
e)The thread calls yield().
Before the call, this thread is alive and running. Immediately after, it is NOT running. But perhaps immediately after that, it may be running again.
So what do I think is the right answer? I don't know!
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Alton Hernandez:
I'm always get stumped by these type of questions because I am not sure if they are asking before or after the fact:

Tell me about it

So what do I think is the right answer? I don't know!

Well.. you are pretty much concluding the same as what I did, so now we are two with the same doubt . :roll:
[ July 01, 2003: Message edited by: Andres Gonzalez ]
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
The questions was "In which of the following cases a thread will definitely be alive but not be running?"
Answer C: "A higher priority thread comes in ready to run state." is NOT correct. Because the priorities are used just for optimisation, they do NOT garantee anything. It is platform dependent. And we should never rely on any logic based on priorities. It is very possible that a thread with lower priority is running, while a thread with higher priority is waiting (I mean stays in the ready-to-run-state).
Answer D "The thread is sleeping as a result of a call to the sleep( ) method." is also correct. Beacause the thread is definately alive (just that is sleeps), and indeed it took the lock with him (but in this question nobody cares). And is definately not running (since it is sleeping). At least for the time specified in the argument(s) of the sleep() method.
Did it help?
Miki


Miki<br /> <br />SCJP 1.4, SCBCD 1.3
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Originally posted by Miki Muzsi:
The questions was "In which of the following cases a thread will definitely be alive but not be running?"
Answer C: "A higher priority thread comes in ready to run state." is NOT correct. Because the priorities are used just for optimisation, they do NOT garantee anything. It is platform dependent. And we should never rely on any logic based on priorities. It is very possible that a thread with lower priority is running, while a thread with higher priority is waiting (I mean stays in the ready-to-run-state).

Thanks miki.. I agree with you with priorities and all that sort of stuff, but as you wrote in the last line, ready-to-run-state,which is the same as the one I mentioned before, runnable. When a thread is in runnable-running-(block/wait/sleep) state it is considered to be alive. That's why I think option C is correct.
the only states when a thread is not alive is new (you created a new thread but haven't started) and dead (thread finished).
does that make sense? let me know..
[ July 01, 2003: Message edited by: Andres Gonzalez ]
Miki Muzsi
Ranch Hand

Joined: Jun 23, 2003
Posts: 120
Andres, it makes perfectly sense what you say and now I see the problem/confusion. I'll try to explain what I think the missunderstanding is, by rephrasing the question:
In which of the following cases a thread (let's name it X) will definitely be alive but not be running (I would add: assuming the thread X is currently running)
And the answer C: A higher priority thread (let's name it Y) comes in ready to run state.
I think the auter reffered to C as incorrect meaning that the thread Y will NOT STOP thread X from its execution (i.e. putting it in a state of: "definitely be alive but not be running" ) just because Y has a higher priority than X.
Your explanation is perfectly valid for thread Y (that a thread is alive and it is in runnable state)! But the questions is about X, in the sense how can you put X from a running state into the runnable state.
Did I explain it well enough?
Miki
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hmm.. looking it from that perspective,and adding what you just added:
"assuming the thread X is currently running"
I agree (shake hands ). I think this question must be rephrased...
thanks for your help
cheers
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
a The thread has issued a call to wait( ).
YES, wait puts the thread into the "Waiting" state.

b The thread is trying to enter a synchronized block and the monitor is not free.
YES, the thread enters the "Blocked" state

c A higher priority thread comes in ready to run state.
NO, you should learn quickly that this is an implementation detail of the thread scheduler, and you should read over and over again that there is no guarnatee the JVM you're using will behave this way.

d The thread is sleeping as a result of a call to the sleep( ) method.
YES, the thread is in a "Not Runnable" state.

e The thread calls yield().
NO, again, the behavior of yield depends on the implementation, so you can't say for certain what will happen here.
P.S. The state names I used are not written in stone. I use what works for me.
[ July 01, 2003: Message edited by: Brian Joseph ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Good thinking Andres. You are right about c. When that higher priority thread becomes ready to run, who knows what the scheduler is going to do next.
A lower priority thread might run to prevent starvation - a feature known as priority aging.
[ July 01, 2003: Message edited by: Marlene Miller ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on threads
 
Similar Threads
Interrupt method + volatile modifier?
join() mock exam question
yield or join
The conflicting answers for these 2 questions!
Threads - doubt...