Created
December 3, 2023 19:42
-
-
Save pablohdzvizcarra/f5563cb6a35292cfa4dec5930b8e44f4 to your computer and use it in GitHub Desktop.
Tutorial: What is a Java Executor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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