| Author |
mock question
|
dipayan chatterjee
Ranch Hand
Joined: Oct 03, 2007
Posts: 47
|
|
hello all Which one statement is always true about the following application? 1. class HiPri extends Thread { 2. HiPri() { 3. setPriority(10); 4. } 5. 6. public void run() { 7. System.out.println( 8. �Another thread starting up.�); 9. while (true) { } 10. } 11. 12. public static void main(String args[]) { 13. HiPri hp1 = new HiPri(); 14. HiPri hp2 = new HiPri(); 15. HiPri hp3 = new HiPri(); 16. hp1.start(); 17. hp2.start(); 18. hp3.start(); 19. } 20. } A. When the application is run, thread hp1 will execute; threads hp2 and hp3 will never get the CPU. B. When the application is run, thread hp1 will execute to completion, thread hp2 will execute to completion, then thread hp3 will execute to completion. C. When the application is run, all three threads (hp1, hp2, and hp3) will execute concurrently, taking time-sliced turns in the CPU. D. None of the above scenarios can be guaranteed to happen in all cases. the answer is C but in the code there is an infinite loop in line 9 in the run method as a result when any one of the 3 threads execute there run methods it is supposed to cling to the CPU and as there is no yeild function it will just not allow any other thread to get the CPU ,so for me the option D seems to be correct because if we run this code on a processor that does not have a time slice arcitecture then C is not correct.please correct me if i am wrong
|
SCJP1.4 , gearing up for SCWCD 5
|
 |
ahmed yehia
Ranch Hand
Joined: Apr 22, 2006
Posts: 424
|
|
in the code there is an infinite loop in line 9 in the run method as a result when any one of the 3 threads execute there run methods it is supposed to cling to the CPU and as there is no yeild function it will just not allow any other thread to get the CPU
Running an infinite loop inside the run method doesnt prevent other threads from taking their turns. Even without yield().
|
 |
dipayan chatterjee
Ranch Hand
Joined: Oct 03, 2007
Posts: 47
|
|
hi ahmed yes you are right that infinite loops do not prevent threads from taking there turns but in K&B it's written that "JVM makes no promises that each thread will take up its turn nicely" so accordingly the option C is still unsuitable
|
 |
 |
|
|
subject: mock question
|
|
|