Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread Question

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from Jitendra Kumar's Exam applet (SET 1, Question # 3)
class Counter {
public int startHere = 1;
public int endHere = 100;
public static void main(String [] args) {
new Counter1().go();
}
void go() {
// A
Thread t = new Thread(a);
t.start();
}
}
What block of code can you replace at line A above so that this program will count from startHere to endHere?
Select all valid answers:
a) Runnable a = new Runnable() {
public void run() {
for(int i = startHere; i<=endHere; i++) {
System.out.println(i);
}
}
};
b) a implements Runnable {
public void run() {
for(int i = startHere; i<=endHere; i++) {
System.out.println(i);
}
}
};
c) Thread a = new Thread() {
public void run() {
for(int i = startHere; i<=endHere; i++) {
System.out.println(i);
}
}
};
Answer is a & c. Why??? Please Help!
Thanks,
TINA
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Tina,
answers a and c create new instances of 'a' and pass it in to Thread as an arguenment. answer b does not create a new instance. The constructor for the Threads used in the original code is: Thread(Runnable a)
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose what you might not be knowing is that Thread implements Runnable
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic