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

Given the code fragment:

public class FileThread implements Runnable {

String fName;

public FileThread(String fName) { this.fName = fName; }

public void run () System.out.println(fName);}

public static void main (String[] args) throws IOException, InterruptedException {

ExecutorService executor = Executors.newCachedThreadPool();

Stream listOfFiles = Files.walk(Paths.get("Java Projects")); listOfFiles.forEach(line -> { executor.execute(new FileThread(line.getFileName().toString())); // line n1

});

executor.shutdown();

executor.awaitTermination(5, TimeUnit.DAYS); // line n2

}

}

The Java Projects directory exists and contains a list of files.

What is the result?

    Correct Answer: B

    The program creates a thread pool using Executors.newCachedThreadPool(), which can reuse previously constructed threads when available or creates new ones as needed. It then iterates through the files in the 'Java Projects' directory and for each file, submits a new task to the executor that prints the file name. As the executor runs these tasks concurrently, file names are printed concurrently. The shutdown method is called to stop the executor from accepting new tasks, and awaitTermination waits up to 5 days for all submitted tasks to complete, ensuring the main thread does not exit before the printing tasks are finished.

Discussion
DestroyerOption: B

Answer is B

M_JawadOption: B

The walk method of the Files class can use depth-first to visit the path of the incoming file. The execute method of the ExecutorService object can use the new thread to execute the passed Runnable object. The shutdown method can close the ExecutorService object so that it no longer accepts new execution objects (Runnable or Callable), but does not wait for the currently executing and queued tasks to complete. The awaitTermination method can specify an upper limit time to wait for the ExecutorService object to finish processing all tasks. answer is B

asdfjhfgjuaDCVOption: B

B is the correct answer

steefaandOption: B

B is answer.