This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

K&B - Practice Exams book - Full Exam II - Question 48

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

I the second full practice exam, question 48, page 224, the correct answers are E and F. I understand F, but E:

E. The output could be: "Eyra", followed by an exception.

I do not really understand. The exception is thrown in the "main" thread which is not caught, but the thread t1 "Andi" has already been started, so it is a live thread and it will continue to the end of its run() method and it will print "Andi" (unless other thread interrupt it, but this is not the case, is it?)

Am I right thinking like that? Probably I misunderstood something.

Thanks a lot,

Alvaro
 
Sheriff
Posts: 7046
1344
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alvaro,
Can you please show us the actual question from the book? Also note that not everyone has that book.
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:


What is the result? (Choose all that apply.)
A. Compilation fails.
B. No output is produced.
C. The output could be: "Andi Eyra "
D. The output could be: "Eyra Andi Eyra "
E. The output could be: "Eyra ", followed by an exception.
F. The output could be: "Eyra Andi ", followed by an exception.
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to say one more thing. I have almost executed 200 times that code and the thread "Andi" always printed its name.
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most important thing you are missing here.

"With threads, very little is guaranteed !! "

When a start() is invoked, the only thing that's guaranteed is that the new thread becomes runnable. It's not guaranteed that it would start running.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alvaro San Millan wrote:Given:



What is the result? (Choose all that apply.)
A. Compilation fails.
B. No output is produced.
C. The output could be: "Andi Eyra "
D. The output could be: "Eyra Andi Eyra "
E. The output could be: "Eyra ", followed by an exception.
F. The output could be: "Eyra Andi ", followed by an exception.



Result:

Exception in thread "main" java.lang.IllegalThreadStateException
Eyra Andi at java.lang.Thread.start(Unknown Source)
at Race.main(Race.java:10)



It given the exception simply because start() method only executed once, and "Each thread will start, and each thread will run to completion.".

The program execute start() twice on t2 instance, see comments in code for details.

Recap from K & B book on Thread chapter page 754:

"Thread instance, just like any other Java object. What you can't do, though, is call start() again.
Once a thread has been started, it can never be started again."


Reread the Thread chapter to clear your doubt and reinforce your knowledge.



Hope it help....
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for your answers. Maybe I did not explain myself well. I know that and IllegalThreadStateException is thrown because the method start() is called twice on thread object t2.

I used to think like you guys, but I am a bit confused because of another question in the "Assessment Test 2" in the book. The question is this one:


Given:


And the two code fragments:

I. if(i == 2 && id == Thread.currentThread().getId()) {
II. if(i == 2) {

When inserting either fragment, independently at line 6, which are true? (Choose all that apply.)
A. Both fragments produce the same output.
B. Both fragments will end in about the same amount of time.
C. Compilation fails, regardless of which fragment is inserted.
D. Regardless of which fragment is inserted, output ends once the Error is thrown.
E. Regardless of which fragment is inserted, output continues after the Error is thrown.

Answer (for Objective 4.2):
- E is correct. In either case, before the Error is thrown a new thread is created, and THE NEW THREAD WILL EXECUTE INDEPENDENTLY OF THE ERROR.
-  A and B are incorrect because fragment II creates a sort of recursive, endless loop. C and D are incorrect based on the above.


I tried this code too, and it is true that after the Error in one of the threads the rest of the threads continue to finish.

So in the case of question 48, the exception is thrown in the "main thread", so the thread t1 "Andi" should finish and print "Andi".

Any more comment about this.

Thanks for your time guys.

Regards,

Alvaro
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again guys,

I have just read this in K&S - Study Guide - Chapter 9 Threads, page 714:

"And there is no guarantee that once a thread starts executing, it will keep executing until it's done. Or that a loop will complete
before another thread begins. No siree Bob. Nothing is guaranteed in the preceding code EXCEPT this: EACH THREAD WILL START, AND EACH THREAD WILL RUN TO COMPLETION".

As Tommy Delson said too:

"It given the exception simply because start() method only executed once, and "Each thread will start, and each thread will run to completion.". "

Is it not clear that thread t1, "Andi" has been started? If yes, will it run to completion?

Honestly, I am really confused.

Regards,

Alvaro
 
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having some chance to happening F also. if you are put main thread in sleep for some seconds you know. but it having the less probability to occurring.

 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think that it is correct. If we put the main thread to sleep, after it wakes up the call t1.start() will happen before the exception is thrown by the call t2.start(). Also, this action will help to thread t1 to print "Andi" if we put the main thread to sleep after the call t1.start(). And here it is the big deal, because the t1 thread ("Andi") has been started, WILL IT RUN TO COMPLETION? As K&S wrote in the Study Guide - Chapter 9 Threads, page 714:

"And there is no guarantee that once a thread starts executing, it will keep executing until it's done. Or that a loop will complete
before another thread begins. No siree Bob. Nothing is guaranteed in the preceding code EXCEPT this: EACH THREAD WILL START, AND EACH THREAD WILL RUN TO COMPLETION".
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's why am said it's having some chance.........

if you want to see that chance means you can given the high priority to remaining threads that's why am asking to put main thread in sleep.
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another possibility to get that chance is also there if you are set the high priority to thread t1 also you get.
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, but I did not understand again. What did you mean when you said "you can given the high priority to remaining threads"? If you set the priority of t1 "Andi" with the highest priority it will have better change to print "Andi", and putting main thread to sleep, simply delay the sequence execution in main thread, depends where you put the call Thread.sleep(). In all cases that you have proposed it is guaranteed that the call t1.start() will execute before second call t2.start(), which is the one that throw the exception.

Anyway, my huge doubt now is: Is it guaranteed that the thread t1 "Andi" which has been started in main thread will run to completion? If so, I do not think option E would be a possibility, because thread t1 will always print "Andi".

If the option E would be like:

E. The output could be: "Eyra ", followed by an exception, followed by "Andi"

In this case, I think option E could be a possibility. But like it stands, without change any code?
 
Naveen Madarapu
Ranch Hand
Posts: 64
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

E. The output could be: "Eyra ", followed by an exception, followed by "Andi"



this is also possibility once see below code.

 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is a possibility. I tried this code and the most probably outputs you get are:

"Eyra Andi ", followed by an exception. OR
"Andi Eyra", followed by an exception. OR
"Eyra", followed by an exception, followed by "Andi" OR
"Andi", followed by an exception, followed by "Eyra"

But, this one

E. The output could be: "Eyra ", followed by an exception.

is not possible because thread t1 will always print "Andi".

Naveen, thanks for your help, seriously. But, could someone answer this question about this code:

Given:



Is it guaranteed that the thread t1 "Andi" which has been started in main thread will run to completion? If so, I do not think option

E. The output could be: "Eyra ", followed by an exception.

would be a possibility, because thread t1 will always print "Andi".

 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing that I tried:




Thread t1 "Andi" run to completion and printed 100 times

XX Andi

where XX is a number between 0 to 99

So, as K&S wrote in the Study Guide - Chapter 9 Threads, page 714:

"And there is no guarantee that once a thread starts executing, it will keep executing until it's done. Or that a loop will complete
before another thread begins. No siree Bob. Nothing is guaranteed in the preceding code EXCEPT this: EACH THREAD WILL START, AND EACH THREAD WILL RUN TO COMPLETION".

Andi thread will run to completion, so option

E. The output could be: "Eyra ", followed by an exception.

is not a possibility.

If we uncomment

// System.exit(0)

in main thread the option E would be a possibility.

Honestly, I am still confused, no one see my view point and it is probably that I am wrong, but it is quite difficult for me to understand this option.

Some help?
 
Alvaro San Millan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The option is possible. Check this thread to understand:

https://coderanch.com/t/546711/java-programmer-SCJP/certification/Will-started-thread-run-completion#2481151
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic