OCPJP 6 (91%)
OCPJP 6 (91%)
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.
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...
3) 3 started hence error because this is already started in (1)
OCPJP 6 (91%)
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 ?
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.
OCPJP 6 (91%)
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.
OCPJP 6 (91%)
Jo Gupta wrote:
Hm... I still don't get why the fact that t1.start(); comes before t2.start(); does NOT ensure that t1 is guaranteed to be "ready to run" and submitted to the scheduler no matter what happens in the main thread.
Because that should ensure that "Andi" is >always< printed.
Henry Wong wrote:
Jo Gupta wrote:
Hm... I still don't get why the fact that t1.start(); comes before t2.start(); does NOT ensure that t1 is guaranteed to be "ready to run" and submitted to the scheduler no matter what happens in the main thread.
Because that should ensure that "Andi" is >always< printed.
So, you are saying that whoever starts a race first should always win?
Henry
Jo Gupta wrote:
No, I was not thinking that. But - once the statement t.start(); has run - isn't that then safe from any side effects of the main Thread, including a second start attempt of t2. t1 is "in its own world" so to speak, detached from main.
Can you advise?
Ankit Gareta wrote:
Whatever can happen, will happen.
Consider Paul's rocket mass heater. |