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 what could be the answer ... 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 "what could be the answer ..." Watch "what could be the answer ..." New topic
Author

what could be the answer ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


what will be the output :
1] true
2] false
3] unpredicted ( no guarantee )

what you will chooose ...

thanks .
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
I will choose 3.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
3 would be a safer to choose.
When t1 thread starts execution, main is still executing, and when it encounters t1.isAlive() , it may not know whether t1 is still executing or not.
So i guess 3 should be safe
sabah ammar
Greenhorn

Joined: Jan 10, 2005
Posts: 23
The safe play is option 3, but what i think is option "true" shall always be correct. because until main thread finishes t1 may not die as the run() has onthing to do.but if u joint t1 with main, after that u may say t1 has to finish before main finishes.
Or may be someone can proove i am wrong. :roll:
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hello,

From my understanding, one should not assume anything about the exact moment a thread ends, even concerning a thread that dies right after its creation (yet in this case, isAlive() will return 'false' most of the time).

My choice so will be 3
Best regards,


Lionel Badiou
CodeFutures Software
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
I would choose 3...BTW if you took this question from a test, what is the right answer?


SCJP 1.4
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
No it was my own question ..
And I am getting continuously true .. although I will also go with 3 ...
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830

[ February 28, 2005: Message edited by: rathi ji ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

42


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
sorry mark , but what is 42 ...
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364


42
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
It seems that we can join any thread in a dead thread . But it will not affect anything ...



please comments ...
Thanks .
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
When you call the join method it checks to see if thread isAlive. If it is alive it waits, if it is not alive it returns. Is there something else you are expecting it to do or affect?


It seems that we can join any thread in a dead thread . But it will not affect anything ...
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I am getting continuously true .. although I will also go with 3 ...

While the JLS may leave the result unpredictable, a particular implementation of the JVM may give a completely predictable result. Another JVM implementation may give a different result.

While the "try it and see" technique has a lot of value, sometimes it can mislead you. When possible, rely on a system's written specifications.


Mike Gershman
SCJP 1.4, SCWCD in process
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830




Now , what you would say ...
Is there any guarantee for any one question ...

My JVM is giving guaranteed output for all the questions which I posted above in previous post ...

Thanks .
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
My JVM is giving guaranteed output for all the questions which I posted above in previous post ...


Excuse me, Rathi, but your JVM is giving consistent results. Only a person can guarantee results. Programs can be consistent but they cannot make promises (except in the movies and in Algorithms class).
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
I guess you need to make sure that you need the concept of thread. First, the execution sequence of a group of Threads is platform dependent. You will consistently get the same results because you always use the same platform. As different platform behaves differently, your program might not work in, say, AIX machines.

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)
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Ok, so what is your answer to this?


What is the output of this program?


1. Compile error
2. Runtime Error
3. true
4. false
5. unpredictable

[ March 01, 2005: Message edited by: Alton Hernandez ]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
OK Mike & Nick ,
I agreed , but can you or anybody please tell me the answer for that two questions , that are tricky enough .
Thanks a lot .
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
42 and 42?
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
When you take the exam, either 1.4 or 1.5, there are always more than 1 answer for thread questions. You need to consider all the possible answers, and thus, the result is always unpredicted, depending on the thread handling on the platform.

Nick
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by rathi ji:
OK Mike & Nick ,
I agreed , but can you or anybody please tell me the answer for that two questions , that are tricky enough .
Thanks a lot .


Yeh, I'm also interested in the answer to my question as well.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
OK OK

Rathi - unpredictable in the formal sense, but the likely output is false if the main thread sleeps while t1 is ready to run.

Alton - runtime error - IllegalThreadStateException
You cannot do this after starting the thread.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Mike Gershman:

Alton - runtime error - IllegalThreadStateException
You cannot do this after starting the thread.


Well you see, here's the dilema. The method setDaemon() uses the isAlive() method.



So, if you say that the isAlive() method is unpredictable then this program may or may not return an "IllegalThreadStateException". However, the API doc clearly states that this method should only be used before the thread is started. So face with the same question in the exam, what should be the answer, unpredictable?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Alton:

The Thread.setDaemon() API states:
This method must be called before the thread is started.

But it also says:
IllegalThreadStateException - if this thread is active.

So, in your code, the result really depends on whether thread t1 finishes before the main thread calls t1.setDaemon()

Calling setDaemon() on a live thread causes an IllegalThreadStateException,
but calling setDaemon() on a dead thread is illegal but is ignored.

Thanks - I didn't realize that.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
In K&B , nothing is given about this setDaemon() method . Does it may come in SCJP 1.4 or tiger-beta exam ???



Mike , If we increase this delay to high value , say 50000 , then also we should choose unpredictable ??

Please reply ...
Thanks a lot .
[ March 02, 2005: Message edited by: rathi ji ]
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
I've been pondering about your question, Rathi. My conclusion is unpredictable, no matter what the sleep time is. A scenario would be the OS is busy with other stuffs and by the time the JVM is given the chance to run, the sleep time for main thread has expired. So, what would be the output? Unpredictable.
[ March 02, 2005: Message edited by: Joyce Lee ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Joyce Lee:
I've been pondering about your question, Rathi. My conclusion is unpredictable, no matter what the sleep time is.


..or where you put the sleep method. Even if you put it under the run() method of HasRunInside, the result is still not guaranteed to be one or the other.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks Joyce ,
I was thinking the same but wanted to confirm ...
Thanks again .
 
 
subject: what could be the answer ...
 
Similar Threads
wait() and notifyAll()
How the Threads work?
Thread
It is too much ....
Creating multiple children threads