Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread practice exam question doubt

 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

I am not having clear at all one of the answer regarding to a practice test from Kathy's and Bert's practice exams book, first question, page 57.

Here the question:



Which are true?
A) Compilation Fails.
B) An exception is thrown at runtime.
C) Bang will execute for a second or two.
D) Bang will execute for at least 10 minutes.
E) Thread t1 will almost certainly be the last thread to finish.
F) Thread t1 will almost certainly be the first thread to finish.
G) It's difficult to predict which thread will be the last to finish.


The correct answers are C and G. I have clear G one but not C. Which is the way to know such as assumption? . Thanks in advance.

 
Wesleigh Pieters
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because line 21 will be called at some point
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Thread's sleep method is static,so t1.sleep() means only the current thread that's executing will go to sleep..

In this example, all 3 thread will execute and all 3 will go to sleep at their i's value reach to 250 once, and sleep to 600 milliseconds after 600 milliseconds they will executing remaining for loop.
Now it can be possible that they all can execute within 1 sec or little more..(can't say exactly).
but as options said it's possible to finish within that time, but not as 10 minutes (Its too long time to finish).

hope that will help,

Thanks,
Ankit
 
Himai Minh
Ranch Hand
Posts: 1328
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the worst case scenario, t1, t2 and t3 execute sequentially. The scheduler let one thread complete before another one start. This is the worst case.
In this worst case scenario, t1 sleeps for 600 ms , completes. Then, t2 sleeps for another 600 ms, and completes. Lastly, t3 sleeps for another 600 ms and completes.

600ms*3 = 1800ms.

The total execution time for Bangs is at least 1800ms + the time to execute the loop.
So, two seconds may be one of the answers.
 
David Samer
Ranch Hand
Posts: 49
Eclipse IDE Opera Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank makes sense now. Thank you to the 3 of you , help much appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic