Challenge: Executors
Task
Imagine that you have a list of files, each of which contains lines of text. Your task is to read lines from these files in parallel and count the number of words in each line. Use ExecutorService, Future, and a thread pool to accomplish the task.
Requirements:
-
Create a thread pool: Create a fixed-size thread pool using
Executors.newFixedThreadPool(int nThreads), wherenThreadsis the number of threads in the pool; -
Task Implementation: Implement a
WordCountTaskclass that implements theCallable<String>interface and receives a file as input. In the call method of this class, read lines from the file, count the number of words in each line, and return the result; -
Using
ExecutorService: UsingExecutorService, send jobs to the thread pool for execution. The jobs must use theWordCountTaskclass.
Also, the string that you form in a separate thread, you must return and use the get() method of the Future interface to get the result of the thread execution and output to the console in the main method.
Read from file
To implement the call() method, you can use BufferedReader to read strings from files. Employ a try-with-resources block so that resources are automatically closed for you.
Main.java
1BufferedReader reader = new BufferedReader(new FileReader(file));
Use the readLine() method to read each line, and be sure to check for null to determine when you've reached the end of the file.
Next, split the string into an array of strings using the split("\\s+") method with the \\s+ pattern, which separates the string by spaces, and then get the size of the array.
In the string itself, which the stream returns, use this formatting
Main.java
1String.format("File: %s | Line: %s | Word Count %d", fileName, line, countWord)
Files are already in the project you don't need to create or modify them!
Example output
File: file1.txt | Line: Hello | Word Count: 1
File: file2.txt | Line: Hello I Love Java | Word Count: 4
File: file2.txt | Line: Lol Hello | Word Count: 2
File: file3.txt | Line: Java so so so so very nice | Word Count: 7
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you show me how to implement the WordCountTask class?
How do I use ExecutorService to run the WordCountTask for multiple files?
Can you explain how to format the output for each line as shown in the example?
Awesome!
Completion rate improved to 3.33
Challenge: Executors
Swipe to show menu
Task
Imagine that you have a list of files, each of which contains lines of text. Your task is to read lines from these files in parallel and count the number of words in each line. Use ExecutorService, Future, and a thread pool to accomplish the task.
Requirements:
-
Create a thread pool: Create a fixed-size thread pool using
Executors.newFixedThreadPool(int nThreads), wherenThreadsis the number of threads in the pool; -
Task Implementation: Implement a
WordCountTaskclass that implements theCallable<String>interface and receives a file as input. In the call method of this class, read lines from the file, count the number of words in each line, and return the result; -
Using
ExecutorService: UsingExecutorService, send jobs to the thread pool for execution. The jobs must use theWordCountTaskclass.
Also, the string that you form in a separate thread, you must return and use the get() method of the Future interface to get the result of the thread execution and output to the console in the main method.
Read from file
To implement the call() method, you can use BufferedReader to read strings from files. Employ a try-with-resources block so that resources are automatically closed for you.
Main.java
1BufferedReader reader = new BufferedReader(new FileReader(file));
Use the readLine() method to read each line, and be sure to check for null to determine when you've reached the end of the file.
Next, split the string into an array of strings using the split("\\s+") method with the \\s+ pattern, which separates the string by spaces, and then get the size of the array.
In the string itself, which the stream returns, use this formatting
Main.java
1String.format("File: %s | Line: %s | Word Count %d", fileName, line, countWord)
Files are already in the project you don't need to create or modify them!
Example output
File: file1.txt | Line: Hello | Word Count: 1
File: file2.txt | Line: Hello I Love Java | Word Count: 4
File: file2.txt | Line: Lol Hello | Word Count: 2
File: file3.txt | Line: Java so so so so very nice | Word Count: 7
Thanks for your feedback!