This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B - Practice Exams book - Full Exam II - Question 48 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B - Practice Exams book - Full Exam II - Question 48" Watch "K&B - Practice Exams book - Full Exam II - Question 48" New topic
Author

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

Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
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


OCPJP 6 98%
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3028
    
  35

Alvaro,
Can you please show us the actual question from the book? Also note that not everyone has that book.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
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

Joined: Mar 31, 2011
Posts: 36
I forgot to say one more thing. I have almost executed 200 times that code and the thread "Andi" always printed its name.
nitin sethi
Ranch Hand

Joined: Jul 14, 2011
Posts: 59

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.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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....


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
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

Joined: Mar 31, 2011
Posts: 36
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
Naveen Madarapu
Ranch Hand

Joined: May 24, 2011
Posts: 64

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.


OCPJP
Alvaro San Millan
Ranch Hand

Joined: Mar 31, 2011
Posts: 36
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

Joined: May 24, 2011
Posts: 64

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

Joined: May 24, 2011
Posts: 64

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

Joined: Mar 31, 2011
Posts: 36
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

Joined: May 24, 2011
Posts: 64

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

Joined: Mar 31, 2011
Posts: 36
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

Joined: Mar 31, 2011
Posts: 36
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

Joined: Mar 31, 2011
Posts: 36
The option is possible. Check this thread to understand:

http://www.coderanch.com/t/546711/java-programmer-SCJP/certification/Will-started-thread-run-completion#2481151
 
Consider Paul's rocket mass heater.
 
subject: K&B - Practice Exams book - Full Exam II - Question 48
 
Similar Threads
Question about the join method in Thread
Will a started thread run to completion in this example?
Is it guaranteed a started thread will run to completion?
Threads question from K&B Practice Exams
Thread question from K&B book