Skip to main content

Java

References

Solutions

ThreadPoolExecutor

Good Example: https://www.journaldev.com/1069/threadpoolexecutor-java-thread-pool-example-executorservice

Pass a List <objects that implement an interface> to a Method

https://stackoverflow.com/questions/10090167/how-can-you-pass-a-listobjects-that-implement-an-interface-to-a-method

Use: List<? extends YourInterface>

public Interface JsonEnabled {
public String getAsJson();
}

public class User implements JsonEnabled {
....
@Override
public String getAsJson() {...}
}

public class TheServlet {
...
private String getUserListAsJson() {
List<User> userList = this.dao.getUsers();
return this.getListAsJson(userList);
}

private String getListAsJson(List<? extends JsonEnabled> list) { ... }
// The loop code that is in each method.
}
}

Thread Sleep

Example of passing “……” to indicate wait times: https://www.rabbitmq.com/tutorials/tutorial-two-java.html

private static void doWork(String task) throws InterruptedException {
for (char ch: task.toCharArray()) {
if (ch == '.') Thread.sleep(1000);
}
}