jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green Mock 3 Q49 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Mock 3 Q49" Watch "Marcus Green Mock 3 Q49" New topic

Marcus Green Mock 3 Q49

chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
Given a reference called t to a class which extends Thread, which of the following will cause it to give up cycles to allow another thread to execute?
1) t.yield();
2) yield();
3) yield(100);
4) yield(t);
The answer says 1 and 2. i'm confused about the inclusion of option 2.
My reasoning:

From the example above i don't think t is paused..
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
Hi chafule
read the que.
>>a reference called t to a class which extends Thread
and you are calling yield() on t.
In your example you are creating two threads. One is executing default run() of Thread class and other is executing overridden run() of S class.
and when you call s.yield() it puts S's run() to ready to run state giving other thread chance to execute, while Thread's run() might be running.
AW your code won't compile as you are not assigning any object to t ref.
(I liked the signature "I don't have compiler else I would have tried.")
[ March 03, 2002: Message edited by: Ravish Kumar ]

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

Besides, yield is a static method of of class Thread, can be called from another class static method main without requiring any object reference. Right?
- Manish
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
Sorry if i provided an inaccurate example

Ravish, what you provided did not answer the question.
What i was asking was that by calling yield() on the current thread it does not mean that the thread object referenced by t will pause and let other thread run.
calling yield() on the current thread may in effect cause the current thread to pause and let the thread referenced by t run ( as opposed to change t's state into non-runnable)
Anyone else please feel free to correct me.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I agree Chafule
yield is static so it's the same call it on any object, even with no one and from within a static context: the current thread would pass to ready state.
Thus both 1 and 2 could be either well or wrong depending if the running thread is the one pointed by t.
I guess question is not very good.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link: http://aspose.com/file-tools
subject: Marcus Green Mock 3 Q49
It's not a secret anymore!