Skip to content

Instantly share code, notes, and snippets.

@pablohdzvizcarra
Created December 3, 2023 19:42
Show Gist options
  • Save pablohdzvizcarra/f5563cb6a35292cfa4dec5930b8e44f4 to your computer and use it in GitHub Desktop.
Save pablohdzvizcarra/f5563cb6a35292cfa4dec5930b8e44f4 to your computer and use it in GitHub Desktop.
Tutorial: What is a Java Executor
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ExecuteTaskInSeparateThread {
private static final Logger logger = Logger.getLogger("App");
public static void main(String[] args) {
logger.info("Starting application");
Thread thread = new Thread(() -> makeHttpRequest());
thread.start();
logger.info("Finish program");
}
private static void makeHttpRequest() {
try {
URL urlToRequest = new URI("https://jsonplaceholder.typicode.com/todos/1").toURL();
HttpURLConnection connection = (HttpURLConnection) urlToRequest.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
logger.log(Level.INFO, "Response code: {0}", responseCode);
} catch (URISyntaxException | IOException e) {
logger.warning(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment