File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green #3 Question 44 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green #3 Question 44" Watch "Marcus Green #3 Question 44" New topic
Author

Marcus Green #3 Question 44

John Towell
Greenhorn

Joined: Jul 26, 2002
Posts: 20
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) yield();
3) yield(100); //Or some other suitable amount in milliseconds
4) yield(t);
Answer 1,2
Why is number 2 there? To call yield statically don't you need to do Thread.yield().
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Maybe the author implied that yield() was inside the class extending Thread.


SCJP2. Please Indent your code using UBB Code
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi John,Jose:
The question does state that the class referred by t extends Thread. The yield() method is static, so assuming it's not called in a static method, option (2) is viable, 'coz the keyword this is implied.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The question doesn't say where the call yield() is. The compiler will complain if no present in a class extending from Thread.
I guess the intention of the question is only totest the signature of yield.
John Towell
Greenhorn

Joined: Jul 26, 2002
Posts: 20
Right it just says that you are given a reference to t, not that you are inside that class. But for the record you can call a static method from a static method Paul right?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
This isn't a well-worded question. Does its author participate here at JavaRanch?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You silly gooses!
The question asks which of the following will cause t to give up cycles. In order for t to give up cycles it must be the object currently executing!!! Marcus' question is perfectly worded if you understand that yield only yields the currently running process.
[ August 14, 2002: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Towell
Greenhorn

Joined: Jul 26, 2002
Posts: 20
Thomas I don't think you understand the question. I like this explanation better.
From http://www.jchq.net/discus/
"This is a known bad question, had been discussed in this forum many times. My guess is Mr. Green is just too busy to correct it. It is correct only in some very special case. Detailed analysis and sample code here: "
http://bobcat.webappcabaret.net/javachina/faq/05.htm#thr_b5
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
John,
I do understand the question. Read the explanation of the yield method: "Causes the currently executing thread object to temporarily pause and allow other threads to execute. "
In the example, t.yield() causes the thread running the main() method to yield, not t!!! How do we know this? Because the main() method must be the currently executing thread.
This is an important point to note. You can never force any thread other than yourself to yield.
[ August 14, 2002: Message edited by: Thomas Paul ]
John Towell
Greenhorn

Joined: Jul 26, 2002
Posts: 20
Thomas,
I don't see a main method in the question? I think I understand where you are coming from. yield() is static method and will cause the currentThread to yield even if you call it on another thread instance. However option 2, I believe is still not always valid. Look at the code below.
<code>
public class ThreadTest extends Thread {
public static void main(String args[]) {
Thread t = new ThreadTest();
t.start();
t.yield(); //1
Thread.yield(); //2
yield(); //3
}

public void run() {
NotAThread nat = new NotAThread();
nat.someMethod(this);
}
}
public class NotAThread {

public void someMethod(Thread t) {
t.yield(); //4
Thread.yield(); //5
yield(); //6 not compile right?
}
}
<\code>
1,2, and 3 will all yield the main thread. And 4 and 5 will yield the Thread spawned from main. However number 6 will not compile because NotAThread does not extend from Thread. Therefore in this case option 2 would not be valid. I haven't compiled or run any of this so it's just speculation.
[ August 14, 2002: Message edited by: John Towell ]
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi all,
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.

I have to admit, the question is ambiguous. But from its wording, I guess you have assume that the thread referenced by t is the one executing, 'coz it can't 'give up cycles' otherwise.
Forget what I said about the yield() method being called from a static method. I didn't assume that yield() would be called from a class that extends Thread.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I don't like the wording of the question either.
If the intention of the author was to test valid invocations regarding the signature of the yield method,it wouldn't have been better a question like this?

Mark those valid signatures for the yield method?

This conveys the idea that the placement of the answer yield() is not be considered in this question.
If the intention of the author was to test what thread was going to be affected by the call: Because the answer depends on which thread is currently running, the question should have mentioned what thread is executing the commands. A less ambigous question could be:

Let t1 and t2 be threads, and t1 is executing the following calls. Mark those that will possibly make t1 to yield?
1) t1.yield();
2) t2.yield();
3) Thread.yield();
4) yield();

Using these accurate statements it wouldn't have been necessary to suppose that t is currently running or that yield() is within a class extending Thread.
[ August 15, 2002: Message edited by: Jose Botella ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The question asks you to determine which methods will cause t to yield cycles. The first thing you must realize is that t must be the currently running thread. Only the currently running thread can give up cycles AND only the currently running thread can execute statements. Therefore the yield statement must be running in t.
What do you think happens given this scenario?
You have three threads: t1, t2, t3. Thread t3 wants thread t1 to yield so it runs, t1.yield(). Which thread yields?
John, the main method was in the example that you supplied a link to! Your example does demonstrate that yield() only works inside of a Thread object. So your example may be considered a special case where yield() doesn't work. By the way, C# avoids this confusion by forcing you to always run static methods off a class name and never an intance name. In C# you would always code Thread.yield().
[ August 15, 2002: Message edited by: Thomas Paul ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Maybe if we reword Jose's question it will be a bit clearer:

Let t1 and t2 be threads, and t1 is executing the following calls. Mark those that will possibly make t2 to yield?
1) t1.yield();
2) t2.yield();
3) Thread.yield();
4) yield();

Correct answer? None of the above.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Paul, the posts before your first message suggest that most of us agreed that the question was badly expressed because it didn't mention where "yield()" was placed.
After your message the mess is up.
Because you guessed that the intention of the question was to test which thread will be acted on you assumed that all of us ignore how yield works.
If the intention of the question is this, t.yield() executed by t, will elicit the same examinee-respose whether you are aware of how yield works or not. In order to try to test this knowledge something like t1.yield() -when t2 is running- would have been much more effective.
Do you think that these posts would have ever been made if the question had been stated as the one you wrote?
[ August 15, 2002: Message edited by: Jose Botella ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I tried writing to Marcus Green in order to point out this thread, but he now refuses e-mail sent to questions@jchq.net and feedback@jchq.net .
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Unfortunatly 90% of emails to those addresses was Klez generated each one around 100K which resulted in frequent temporary suspension of my real email address as the in box filled up. Note that I have never used a Microsoft email client for my personal mail and I do not use a Microsoft operating system. Yet I suffer from the culpable lack of security and thought that goes into those products. If you think that convenience is more important than security, please post your email address.
But to (very briefly) answer the real question, I believe the question is not as well phrased as it should be and I would defer to Roseannes analysis (actually I generally defer to Roseannes analysis on most things).
I will be re-phrasing the question on the site and I'll post here when I have done it.
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Originally posted by Jose Botella:
Paul, the posts before your first message suggest that most of us agreed that the question was badly expressed because it didn't mention where "yield()" was placed.

Ok...
After your message the mess is up.
Because you guessed that the intention of the question was to test which thread will be acted on you assumed that all of us ignore how yield works.

I never said anything like that! All I wanted to say is that
(1) The class referneced by t extends Thread, and
(2) You can call yield() without explicitly specifying the class name (Thread).
Okay, so the part about yield() being called from a static method was horribly wrong, but I have no idea how you managed to blow my statements way out of proportion.
If the intention of the question is this, t.yield() executed by t, will elicit the same examinee-respose whether you are aware of how yield works or not. In order to try to test this knowledge something like t1.yield() -when t2 is running- would have been much more effective.

Do you think that these posts would have ever been made if the question had been stated as the one you wrote?
[ August 15, 2002: Message edited by: Jose Botella ]

I just copied the original question word-for-word!
I don't know why you're blaming me for all this, I just put my two cents' worth. Is there a new policy at Javaranch where you can't post incorrect answers?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Paul - When he said "Paul" he may have meant me. And congrats on your 100th post!
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Lol! And I was so worked up over this, too.
And thanks for the congrats! I wasn't keeping count, guess I've been enjoying my stay too much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green #3 Question 44
 
Similar Threads
Please, help! I'm a bit confused :)
Marcus
Marcus Green #3, Q44
Marcus Green: incorrect option?
Marcus green test#3, question #44