• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Thread

 
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't think the question is bothering to mention what happens after any exceptions.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


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

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
Bartender
Posts: 2446
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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.

Can someone advise?

Thank you in advance!
JG
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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



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?

Thank you in advance,
JG
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



How a thread runs is determined by the scheduler -- which will vary from OS to OS, and how the OS is loaded (including running other applications). Terms like "safe from any side effects", and "in its own world" sounds interesting, but are they defined anywhere? Basically, "ensure" and "always" are very strong terms, which arguably, needs some sort of guarantee to make it so.

The Java specification makes no such claims. In fact, it even goes to mention that there is no such guarantee, and is dependent by the underlying OS and hardware.

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

Ankit Gareta wrote:


Consider the following:
1. At line 8, thread t2 acquires the processor (which was earlier held by main thread), executes and prints "Eyra" and then ceases. Main thread acquires the processor again.
2. Main thread continues from line 9 and starts thread t1 but this time, main thread continues to hold the processor and continues its execution from line 10.
3. Thread t1 is starts but not even a single statement of its Run() method is executes.
4. Main thread executes line 10 and exception arises.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic