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 Thread 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 "Thread" Watch "Thread" New topic
Author

Thread

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi All,

OCP Java SE 6 Programmer practice exam --> practice exam 2 --> Question No 48



here I can't understand why the answer E is correct, if Andi thread run first it will print "Andi", and then run eyra thread and print "Eyra" and then exception, and if the method goes execute first then it prints Eyra and then Andi and then exception, i can't understand how E will be the answer.

Thanks in advance.


OCPJP 6 (91%)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

The exception is thrown when you try to start t2 for the second time, right? Why are you sure that t1 will run before that happens?
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi Matthew ,thanks for reply

what I am thinking is..

here t1.start() comes first and then t2.start()
so when the code comes for line 10 for starting t2 again at that time , t1 will start already... and if exception throws earlier then t1 execute then answer should be 'The output could be: "Eyra", followed by an exception, "Andi" ' rather than answer E
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

There's a case you haven't considered. As soon as t1 is started, control passes back to the main thread and t2 is started before t1 prints anything. Think case 1, but with labels 2 and 3 swapped.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

Try it this way

An exception happens as soon as C is executed. What are the constraints on these steps? A -> B -> C must be in that order. A must be before E. B must be before D. Any other order is possible. Answer E corresponds to the sequences A -> B -> E -> C -> D and A -> E -> B -> C -> D.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi matthew ,

Matthew Brown wrote:Try it this way

An exception happens as soon as C is executed. What are the constraints on these steps? A -> B -> C must be in that order. A must be before E. B must be before D. Any other order is possible. Answer E corresponds to the sequences A -> B -> E -> C -> D and A -> E -> B -> C -> D.


so in that case, D comes last and it running on different stack, after exception , doesn't it print "Andi" in last?
so, why not this should be answer : "Eyra" , exception throws , "Andi".
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

I just don't think the question is bothering to mention what happens after any exceptions.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

new Race().go(t2); --- 1
t1.start(); ----- 2
t2.start(); ----- 3

the above execution order may change because, threads are interleaved.

for instance consider below possibility...

1) 1 enters run and print *Eyra*.

2) 2 enters its run before printing(sometime even it wont reach run) and it controls went to another thread say main

3) 3 started hence error because this is already started in (1)

so, answer E is valid. right?

* remember if you get any question from thread, read 2 times given answer and pick accordingly...
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks Matthew and Seetharaman for your reply...

Seetharaman Venkatasamy wrote:new Race().go(t2); --- 1
t1.start(); ----- 2
t2.start(); ----- 3

the above execution order may change because, threads are interleaved.

for instance consider below possibility...

1) 1 enters run and print *Eyra*.

2) 2 enters its run before printing and it controls went to another thread say main

3) 3 started hence error because this is already started in (1)

so, answer E is valid. right?

* remember if you get any question from thread, read 2 times given answer and pick accordingly...


what i think is , after
3) 3 started hence error because this is already started in (1)

exception thrown , still thread t1 that started earlier but didn't print that should print "Andi".
Question is : "what's the result ?" -- should we don't bother what's still in result in this type of question in exam after exception thrown ?

please, correct me if i wrong.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ankit Gareta wrote:
Question is : "what's the result ?" -- should we don't bother what's still in result in this type of question in exam after exception thrown ?

I understand your worry, but I dont know...may be some other rancher can help you ...
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758


When new Race().go(t2) executes , t2 starts. t1.start() executes. But it does not mean the run() method is executed right away. t2.start() throws an exception because t2 has already started.
"Andi" has not printed yet when the exception is thrown.
To my understanding, a thread starts , but it may not execut the run() method right away. KB's book mentions this point.

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks for your reply Himai and Seetharaman.

Himai Minh wrote:
To my understanding, a thread starts , but it may not execut the run() method right away. KB's book mentions this point.


May be I missed that point, but can you please let me know where this point mentions in KB's book ?

Thanks in Advance.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
In KB's book, it mentioned that a thread starts, but it does not mean the thread will execute right away.
In the book, it says when a thread starts, it is in runnable state. When the scheduler select it to run, it will execute the run() method.
See figure 9.2 description in KB's book.

You can start three threads, but the scheduler only schedule one thread to execute at a time. The other two threads are in runnable state, but wait until the first thread finishes its execution at that point of time.
For example, in a non-thread safe environment, thread 1 executes a line of a code at this moment, thread 2 and 3 have started, but wait until thread 1 completes the line of code. Then, the scheduler gives thread 2 the chance to run and so on.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Himai Minh wrote:In KB's book, it mentioned that a thread starts, but it does not mean the thread will execute right away.
In the book, it says when a thread starts, it is in runnable state. When the scheduler select it to run, it will execute the run() method.
See figure 9.2 description in KB's book.

You can start three threads, but the scheduler only schedule one thread to execute at a time. The other two threads are in runnable state, but wait until the first thread finishes its execution at that point of time.
For example, in a non-thread safe environment, thread 1 executes a line of a code at this moment, thread 2 and 3 have started, but wait until thread 1 completes the line of code. Then, the scheduler gives thread 2 the chance to run and so on.


so, when the thread is on runnable state, doesn't it get its own stack ?
because the exception throws on main thread and if the thread t1 is in runnable state, t1 has its own stack it will execute when it will get chance to run.

please correct me if I am wrong.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
Yes. A thread in a runnable state has its own stack.
In the program, t2 is started twice. For the second time t2 is started, the main thread throws an exception.

The possible steps :
1. t2 starts and prints "Eyra"
2. t1 starts, but has not printed "Andi" yet.
3. t2 is started for a second time. Then, the main thread throws an exception.

One of the answer is E.
Pingling Chen
Greenhorn

Joined: Jan 26, 2013
Posts: 4
Kindly correct me if I'm mistaken:
1. Eventually result will include one "Eyra", one "Andi" and one exception.
2. All kinds of ordering might happen.
 
wood burning stoves
 
subject: Thread