Consider adding a doc comment for doWork
43 System.out.println("Sum: " + sum);
44 }
45
46 private static int doWork(int id) {47 // Simulate some work48 try {49 Thread.sleep(1000);50 } catch (InterruptedException e) {51 e.printStackTrace();52 }53 return id * 2;54 }55}
Consider adding a doc comment for main
3
4public class AdvancedConcurrency {
5
6 public static void main(String[] args) { 7 // Create an ExecutorService with a fixed thread pool 8 ExecutorService executor = Executors.newFixedThreadPool(4); 910 // Submit Callable tasks to the ExecutorService11 List<Callable<Integer>> tasks =12 List.of(13 () -> {14 return doWork(1);15 },16 () -> {17 return doWork(2);18 },19 () -> {20 return doWork(3);21 },22 () -> {23 return doWork(4);24 });2526 try {27 // Invoke all tasks and collect results in a List<Future<Integer>>28 List<Future<Integer>> results = executor.invokeAll(tasks);2930 // Process results31 for (Future<Integer> result : results) {32 System.out.println(result.get());33 }34 } catch (InterruptedException | ExecutionException e) {35 e.printStackTrace();36 } finally {37 // Shutdown the ExecutorService38 executor.shutdown();39 }4041 // Parallel Stream example42 int sum = IntStream.range(0, 1000000).parallel().reduce(0, Integer::sum);43 System.out.println("Sum: " + sum);44 }45
46 private static int doWork(int id) {
47 // Simulate some work
Description
This method does not have any documentation.
Consider adding a documentation comment to explain its use.
While it may seem like the usage of a method is perfectly obvious, any consumers of your API may not be able to pick up on certain details.
Bad Practice
This method appears to return an address string based on its name and the return type, but it is hard to say anything further about it. We neither know if the address will be formatted in a special way nor are we given any useful information regarding the returned value.
String getAddress() {
// ...
}
Recommended
Provide a detailed description of what the method does.
/**
* Returns the address in 3 line format,
* with street address on line 1, area/city on line 2
* and state and country as well as postal code on line 3.
*/
String getAddress() {
// ...
}