Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 161

Given the code fragments:

class ThreadRunner implements Runnable {

public void run () { System.out.print ("Runnable") ; }

}

class ThreadCaller implements Callable {

Public String call () throws Exception {return "Callable"; )

}

and

ExecutorService es = Executors.newCachedThreadPool ();

Runnable r1 = new ThreadRunner ();

Callable c1 = new ThreadCaller ();

// line n1

es.shutdown();

Which code fragment can be inserted at line n1 to start r1 and c1 threads?

    Correct Answer: D

    To start both r1 and c1 threads using an ExecutorService, you need to use the submit method for both. The submit method is capable of handling both Runnable and Callable instances. The execute method only works with Runnable instances and does not return a Future object. Using submit on r1 will allow it to start running, and using submit on c1 will start it and allow you to retrieve a Future<String> object to get the result later.

Discussion
steefaandOption: D

D is true. Execute method accepts only Runnable, not Callable.

duydnOption: D

Runnable return void Callable can return a type -> D is correct

Svetleto13Option: D

D,tested.It prints Runnable