• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Question on Thread

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following code sample:

public class Holt extends Thread{
private String sThreadName;
public static void main(String argv[]){
Holt h = new Holt();
h.go();
}
Holt(){}

Holt(String s){
sThreadName = s;
}
public String getThreadName(){
return sThreadName;
}

public void go(){
Holt first = new Holt("first");
first.start();
Holt second = new Holt("second");
second.start();
}

public void start(){
for(int i = 0; i < 2; i ++){
System.out.println(getThreadName() +i);
try{
Thread.sleep(100);
} catch(InterruptedException e){System.out.println(e.getMessage());}
}
}
}


1) Compile time error
2) Output of first0, second0, first0, second1
3) Output of first0, first1, second0, second1
4) Runtime error
Ans: 3

Doubt: is it necessary 2 always override the run() OR NOT? Or simple a start will do as in this question?

Is it necessary to have Thread.sleep(100) OR just sleep(100)

pls help,
regards,
gitesh
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://coderanch.com/t/264749/java-programmer-SCJP/certification/Quote-your-sources
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source:

Marcus Green-mock-1 Q31

Pls help..
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gitesh !!

As per i know if we are implementing the
runnable interface than only it is compulsory
for us to override the run method.
Here you are using Thread class so start
method is enough and using
sleep method so i will say you
must use Thread.sleep(1000) instead of using sleep(100)!!!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic