Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green: incorrect option?

 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Question 44)
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) Thread.yield();
3) yield(100); //Or some other suitable amount in milliseconds
4) yield(t);

Answer to Question 44)
Objective 7.1)
yield is a static method and causes whatever thread is currently executing to yield its cycles.
1) t.yield();
2) Thread.yield()
(Thanks Roseanne )


Such a behavior is quite possible, but not guaranteed as the same thread may be chosen to run by the scheduler after yielding. Therefore, I think having this question asking which of the following can cause it to give up cycles to allow another thread to execute is more appropriate.
Marcus Green's exam 3, Question 44
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. In fact, the thread may not even yield at all.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, it is possible that on some virtual machines neither "must" or "can" is appropriate.
"On JVM implementations that employ pre-emptive scheduling policies, especially those on multiprocessors, it is possible and even desirable that the scheduler will simply ignore this hint provided by yield." Concurrent Programming in Java, Lea 1.1.2.5
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that can is OK; it's vague enough either way. Maybe it will never happen on a particular JVM; maybe it will always happen on another JVM; either way, "can" is appropriate, IMO.
There's another problem with this questin which I consider more serious:
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.

What's "it"? The thread represented by t seems most likely though that's not definite. The problem is, ifyou call t.yeild(), that isn't likely to have any effct on t at all - unless t just happens to also be the currently executing thread. I think that this is the sort of thing that the real exam might actually test you on - whether or not you know that yield() is static and therefore most likely has no effect on t. And the question is extremely ambiguous in this regard. The answer is less ambiguous, except that it does say that option 1 is correct - and in many cases, it's not. I suppose that if we add the word "can" as previously suggested, that might be vague enough to allow option 1 to be correct. But that's still misleading. The most likely scenario is that option 1 will have no effect at all on t.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, Jim. I ran across a question on a mock exam just today where the trick was about thread t1 invoking t2.sleep().
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:
Given a reference called t to a class which extends Thread, which of the following may cause the currently executing thread to give up cycles to allow another thread to execute?
[ October 13, 2003: Message edited by: Thomas Paul ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas, I think your statement gives a strong hint as to the answer, namely that t and the currently executing thread are two different ideas.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it should be changed to can, who is this Marcus Green person?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it should be changed to can,
No comment on the other point?
While I might agree with Marlene that Tom's suggested wording make the problem too easy, that's preferable to being wrong of misleading.
Another possible wording is
"which of the following can cause a thread to give up cycles..."
or (going back to the other part)
"which of the following will typically cause a thread to give up cycles..."
who is this Marcus Green person?
I think he's a myth. Who was that masked man?
[ October 13, 2003: Message edited by: Jim Yingst ]
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
who is this Marcus Green person?
he is a legend
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic