Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Sudipta Das
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is also from the mock exam http://jquest.webjump.com. The correct answers are given as 'a' & 'b'. Is only 'b' not enough ? Please advise me.
What can you write at the comment //A in the following code so that this program writes the word "running" to the standard output?
1. class RunTest implements Runnable {
2. public static void main(String args[]) {
3. RunTest rt = new RunTest();
4. Thread t = new Thread(rt);
5. //A
6. }
7. public void run() {
8. System.out.println("running");
9. }
10. void go() {
11. start(1);
12. }
13. void start(int i) {
14. }
15. }
Select all valid answers.

Ans :
a. System.out.println("running");
b. t.start();
c. rt.start();
d. rt.start(1);
 
Sean Casey
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this code answer b is fine. You are implementing the interface Runnable which contains one method to override public void run(). In this definition the run method is properly overridden. A thread object is created and given an instance of the class implementing runnable as it's target, so t.start will move the thread to the runnable state.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic