Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green Mock 3 Q49

 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
eg

From the example above i don't think t is paused..
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.")
CMIW
HTH
[ March 03, 2002: Message edited by: Ravish Kumar ]
 
Manish Hatwalne
Ranch Hand
Posts: 2591
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
HTH,
- Manish
 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
TIA
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic