aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread 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 "Thread" Watch "Thread" New topic
Author

Thread

Tony kunds
Greenhorn

Joined: Apr 25, 2002
Posts: 25
reference t to a class Thread ,which of the following will cause it to give up cycles to allow another thread to execute.
1)t.yeild();
2)yeild();
Ans is 2 .
why not 1.
-thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you look at the API spec for Thread, you'll see the following description in the yield method:

Causes the currently executing thread object to temporarily pause and allow other threads to execute.

You don't call yield on another thread, you have the currently executing thread invoke the method to have it give up processor cycles. Therefore, whenever you'd invoke yield, you'd be within the thread object already, so there's no reason for the reference to t.
I hope that helps,
Corey


SCJP Tipline, etc.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Corey,
I'd agree with you that "there's no reason for the reference to t," but I'm not sure that using the reference to t makes the statement in any way "incorrect." Since yield() is a static method of the Thread class, the statement t.yield() is perfectly legal. To me, this seems to be an issue of style since both of the statements have the same effect. Or, am I misreading what you're saying?

Also, on other thing I want to point out is that given the wording of the question:
which of the following will cause it to give up cycles to allow another thread to execute

I'm not sure that either of the answers listed is really correct. yield() will not definatelycause the JVM to chose a different thread. Rather, invoking yield() introduces the possibility that another thread wil be chosen by the JVM.


Fun programming etcetera!
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Dave Winn:
I'm not sure that either of the answers listed is really correct. yield() will not definatelycause the JVM to chose a different thread. Rather, invoking yield() introduces the possibility that another thread wil be chosen by the JVM.

I agree with this point. Calling yield is really more of a "request" to the JVM to give up the processor. I don't know that it always will. It's a lot like telling the garbage collector to run. You can request it and, most of the time it'll probably happen, but it's not guaranteed. At least, I don't think so.
About the other point, though, I don't think I agree. Look at this example:

In the above example, you can see that yield is called within the run method, by the spawned thread. In that method, t is out of scope. Therefore, calling t.yield would be a compiler error.
I hope that makes sense,
Corey
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Corey,
I quite agree with you that t is not in the scope for the example you list. But, the question doesn't list a context really. I've altered your code a bit to give what, imho, could be an example in which t.yield() is valid. This is the type of scenario of which I was thinking when i said that the debate was a matter of style:

Not necessarliy the best example since starting one thread should eventually result in an infinite number of threads being started, but it's the best i could do off the top of my head. What's importnat, I think, is that t is now a local reference to a Thread object which allows us to use t in a call to the static method yield().
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
But, in that example, you're right back to invoking yield on another thread. That's not really the way yield is intended to function. Look back at my original reply. The yield method should be invoked by the currently executing thread - not by another thread telling this thread to yield.
In any such case, the proper method of calling the thread would be yield() or this.yield(). Perhaps, if you had a member variable named t that had a reference to this, then t.yield would be acceptable. Of course, I have no idea why you'd want to do such a thing.
Corey
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Corey, I see what you are saying now. Apologies for not reading your initial statement carefully enough. I think I must be missing a basic concept somewhere that's preventing me from seeing why calling t.yield() (where t is not the currently running thread) would be so bad. I'm wondering about the following.
I'm making these assumptions:
1) yield() is a static method.
2) static methods can be called from null--i.e. null.yield() should compile.
So, to my mind there's no real difference between saying "null.yield();" and "this.yield()" and "Thread.yield();"
But, now that I think about that a little more, it occurs to me that there probably is some difference; otherwise, why would those different forms exist? But I don't know what the difference is, and i think this must be why i'm confused....could someone please set me right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Dave Winn:
I'm making these assumptions:
1) yield() is a static method.
2) static methods can be called from null--i.e. null.yield() should compile.
So, to my mind there's no real difference between saying "null.yield();" and "this.yield()" and "Thread.yield();"
But, now that I think about that a little more, it occurs to me that there probably is some difference; otherwise, why would those different forms exist? But I don't know what the difference is, and i think this must be why i'm confused....could someone please set me right?

Okay, a couple things here. First of all, the different method invocations will all work, with the exception of null.yield. Look here:

You can't invoke a method on null. You can, however, invoke a static method on a null reference. The reason being is that, if the method to be invoked is static, the JVM looks at the type of the object you're invoking it on and then finds the correct method. In the case above, t has a type, Test. However, null has no type.
From the JLS, §15.12.4.4 Locate Method to Invoke:

The strategy for method lookup depends on the invocation mode.
If the invocation mode is static, no target reference is needed and overriding is not allowed. Method m of class T is the one to be invoked.

So, that said, let's get back to the original question. Why does it make no sense to say t.yield()?
What does yield do? It cuases the thread that's running to stop and allow other threads to execute. Now, what happens if you tell some other thread to yield? What have you accomplished? Nothing - the thread that you told to yield isn't running anyway. If it was, you couldn't call yield from this thread because this thread wouldn't be running. It only makes sense for a thread to call yield on itself. So, unless a thread has a reference to this named t, t.yield won't work.
I hope that helps,
Corey
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
calling t1.yield, t2.yield or Thread.yield will always execute the same code. The one defined in the API for the Thread class. That is, the current thread will received a yield call. Though calling a static method on an instance is ok, here it could be confusing. Still it's valid.


SCJP2. Please Indent your code using UBB Code
andy lau
Ranch Hand

Joined: Apr 08, 2002
Posts: 51
i think the yield() method will cause the current executing thread to stop!
At least,the executing Thread will stop to ''look outside'' and to find whether there is any Thread with the same or higher priority. so i think the yield() method will definitly cause the current executing thread to stop!
am i right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Yes - calling yield will cause the current thread to stop executing (potentially). It is possible that even a call to yield won't result in the thread executing to stop. One example might be a case where the only thread executing is the only one capable of running. Perhaps all other threads are terminated or aren't in a ready-to-run state.
Of course, I never meant to say that t.yield() is illegal. It'll compile and run just fine. However, from the way the original question was worded, it seemed to imply that you were trying to get some other thread to yield. Executing t.yield could, in fact, result in the exact opposite. The current thread (the one you're trying to stop the other thread from) would be caused to stop and the thread that you just tried to stop might very well start running.
Of course, this all comes back to the point of "Why would you want to yield a different thread?" The other thread isn't running, so what would you gain from even trying?
Corey
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
yield could be useful to prevent selfish threads from starving others. Those threads with a long run that don't yield, or sleep, or are not synchronized, could make others thread no to receive enough time to performance theirs tasks.
However yield is discourage when making programs to different platforms, because the scheduling mechanism could be so different in different OS that the same program could not behave in the same manner. On the contrary a program properly synchronized should behave the same in any platform.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread