• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what could be the answer ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

what you will chooose ...

thanks .
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will choose 3.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would choose 3...BTW if you took this question from a test, what is the right answer?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it was my own question ..
And I am getting continuously true .. although I will also go with 3 ...
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ February 28, 2005: Message edited by: rathi ji ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
42
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry mark , but what is 42 ...
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


42
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that we can join any thread in a dead thread . But it will not affect anything ...



please comments ...
Thanks .
 
Carol Enderlin
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...

 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
42 and 42?
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joyce ,
I was thinking the same but wanted to confirm ...
Thanks again .
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic