Exam Prep Practice Questions

Question 1 You are creating a class that extends Object and implements Runnable. You have already written a run method for this class. You need a way to create a thread and have it execute the run method. Which of the following start methods should you use?
  • A.

    public void start(){
        new Thread( this ).start();
     }
    
  • B.

    public void start(){
       Thread myT = new Thread();
       myT.start();
    }
    
  • C.

    public void start(){
       Thread myT = new Thread(this);
       myT.run();
    }
    
A1: Answer A is correct. It correctly creates a new thread attached to the runnable object and starts it. It does not matter that there is no reference to the thread in the start method. Answer B is incorrect because the new thread is not attached to the runnable object, so it ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.