• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reg. Threads : Valiveru's exam

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


Given the code
1public class ThreadDemo extends Thread{
2 public void run(){
3System.out.println("Thread Started");
4//What Code ???
5 System.out.println("Done");
6 }
7 public static void main(String[] arg){
8ThreadDemo td = new ThreadDemo();
9td.start();
10 }
11}
Which of the following given code segment(s) below can be placed at line 4 to print both "Thread Started" and "Done" to the standard output.
A.suspend(); resume();
B.sleep(10000);
C.yield();
D.yield(); resume();
E.All the above


What is the correct answer ?
Also in such questions do we have to consider the exception
handling part ?
( For eg: if i mark sleep(10000) but the code does not
have a try-catch block. Will it be right ? )
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct options r C n D
A) Once suspended() , the current Thread wont move to ready to run state until call to resume() on it , and in this case tht resume in next line ll never be reached ( n worst , compiler wont warn us ! , suspend n resume r depreciated )
B) sleep() throws a checked xception , which is nt catched
E) as A) n B) r incorrect , so is it
hmm... do we hav to consider xception handling part ?
well my best guess is we have to , and don worry in real exam we ll get much worked-out n re-worked options , n there we ll be told how many to choose , so dat shld help take these decisions on da fly

------------------
Gagan (/^_^\)
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Correct answers are C and D.
sleep(10000) w/o a try/catch block is a compiler error. Because sleep() throws InterruptedException which is a checked exception and it has to be caught in the code.

------------------
Asma Zafar,
Sun Certified Programmer for Java2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not clear as to how the option D is correct.
I suppose nothing can be guaranteed about yield()
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the API docs.
Resume()
This method exists solely for use with suspend(), which has been deprecated because it is deadlock-prone
So now I wonder how D would be correct. Can somebody explain.
Roopa.

Originally posted by Asma Zafar:
Yes, Correct answers are C and D.
sleep(10000) w/o a try/catch block is a compiler error. Because sleep() throws InterruptedException which is a checked exception and it has to be caught in the code.


 
reply
    Bookmark Topic Watch Topic
  • New Topic