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 It is too much .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

It is too much ....

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


Every body knows that , when any thread calls yield() method , it has to go in runnable state .

In above example , it is sure that output will be false every time ( I can bat for this ) ...

But In this case also , is it right to choose No guarantee ... It is too much ...

Thanks .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
For this case, I'll still choose "No guarantee". It's possible that the main thread yields and is then reselected by the scheduler especially for threads with same priority (well, that's what K&B book says on pg 513 ).
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Running this program in the BlueJ IDE gives the result true


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Come-on Barry ,
It can't be dependent on IDE ...
It may be because of your JVM .
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Calling yield() puts the thread into Ready state, but some scheduler implementations will ignore the call if it determines that it will only put the thread back into Running state immediately after.

The output of your test case is indeterminate.


Tony Morris
Java Q&A (FAQ, Trivia)
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
Originally posted by rathi ji:
Come-on Barry ,
It can't be dependent on IDE ...
It may be because of your JVM .


I tried the same program in Eclipse and BlueJ with the same jdk and jre and found false and true output respectively. It can't be dependent on IDE as rathi said then why am i getting different output???
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
What do you mean by same JVM and JRE? You just mean the same version, e.g. 1.3.1? If so, there might be problems, as different JVM might give different results.

As you might know, the behavior of JVM in WSAD/WAS is different from the one in SunONE, say, even both declare that they are J2SE 1.3.1.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Like already stated, there are no guarantees so yield might not have the effect of getting the other thread to run at all.
This is not IDE dependent, not JVM dependent, it's pure chance.
One run might produce false, another true. Maybe you get true a thousand times and then false just once, there's just no telling.


42
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Rathi, why did you post this twice? You know our rules about duplicate posting.

I am going to close this thread, since the other one has my really funny response of "42".

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It is too much ....
 
Similar Threads
casting
How the Threads work?
Creating multiple children threads
Clarify Use of the join method
what could be the answer ...