Callable vs Runnable in Java – Key Difference In Java multithreading, both Runnable and Callable are used to define tasks that run in a separate thread. But they are not the same. ✅ Runnable • Introduced in Java 1.0 • Used to execute a task in a thread • Does not return any result • Cannot throw checked exceptions ✅ Callable • Introduced in Java 1.5 (Executor framework) • Used when a task needs to return a result • Can throw checked exceptions • Works with Future to retrieve the result 💡 When to use what? • If you just need to run a background task → use Runnable • If you need a response/result from that task → use Callable In real-world backend applications (especially with thread pools and Executors), Callable provides more flexibility. #Java #Multithreading #SpringBoot #BackendDevelopment #InterviewPreparation
Java Runnable vs Callable: Key Differences
More Relevant Posts
-
Java Evolved: 112 modern patterns · Java 8 → Java 25 TIL: You don't need to create an object within try-with block, but can just do `try(var) {}` and var will be closed after. Should probably take a closer look a the other 111 patterns. https://lnkd.in/es6Rhu46
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 308 🚀 ⸻ 🔹 Day 308: Default Methods in Interfaces Which of the following statements about default methods in Java interfaces is FALSE? Options: A) A default method can provide a concrete implementation that classes inheriting the interface can use without overriding. B) A class can inherit multiple default methods with the same signature from different interfaces without any conflict. C) A default method can be overridden by a concrete class to provide a specific implementation. D) An abstract class that implements an interface is not required to implement the interface’s default methods.
To view or add a comment, sign in
-
🚀 Structure of Multi-Release JAR Files (Java) Multi-release JAR files have a specific directory structure. The base classes are placed in the root of the JAR file. Version-specific classes are placed in a `META-INF/versions/` directory, where `` is the Java version number (e.g., `META-INF/versions/9`). The Java runtime will automatically load the appropriate version of the class based on the current Java version. This allows for seamless compatibility and feature adoption. #Java #JavaDev #OOP #Backend #professional #career #development
To view or add a comment, sign in
-
-
Java Fundamentals Series – Day 9 Exception Handling in Java : Exception Handling helps in handling runtime errors gracefully without crashing the application. 1. What is an Exception? An exception is an unexpected event that disrupts normal program flow. 2. Types of Exceptions: Checked Exceptions – Checked at compile time (e.g., IOException, SQLException) Unchecked Exceptions – Occur at runtime (e.g., NullPointerException, ArithmeticException) Key Keywords: try → wraps risky code catch → handles exception finally → executes always throw → explicitly throws exception throws → declares exception Why Exception Handling is Important? 1. Prevents program crash 2. Improves reliability 3. Helps in debugging 4. Improves user experience #Java #ExceptionHandling #BackendDeveloper #ComputerScience #Placements
To view or add a comment, sign in
-
🚀 365-Day Java Challenge – Day 306 🚀 ⸻ 🔹 Day 306: Lambda expressions and variable capture Given the code: int factor = 2; java.util.function.Function<Integer,Integer> multiplier = x -> x * factor; factor = 3; int result = multiplier.apply(5); What is the value of result? Options: A) 10 B) 15 C) Compilation error D) 5
To view or add a comment, sign in
-
🚀 Java Series – Day 16 📌 Custom Exception Handling in Java 🔹 What is it? Custom exceptions are user-defined exceptions used when built-in exceptions are not enough to handle specific scenarios. 🔹 Why do we use it? They help create clear and meaningful error messages for business logic. For example: In an e-commerce application, we can create an OutOfStockException when a product is unavailable. 🔹 Example: class OutOfStockException extends Exception { public OutOfStockException(String message) { super(message); } } public class Main { public static void main(String[] args) { try { throw new OutOfStockException("Product is out of stock"); } catch (OutOfStockException e) { System.out.println(e.getMessage()); } } } 💡 Key Takeaway: Custom exceptions make your code clean, readable, and business-focused. What do you think about this? 👇 #Java #ExceptionHandling #JavaDeveloper #Programming #BackendDevelopment
To view or add a comment, sign in
-
-
Discover Java 11 features like HTTP Client, var in lambdas, new String methods, and file I/O updates with code and JEP links.
To view or add a comment, sign in
-
🚀Java Tip Java Tip: Use Optional to avoid NullPointerException One of the most common issues developers face in Java applications is the NullPointerException. Java 8 introduced the Optional class to help handle null values more safely and clearly. Instead of directly working with possible null values, Optional provides a container that may or may not contain a value. 🔹 Example without Optional User user = getUser(); String name = user.getName(); // May throw NullPointerException 🔹 Example using Optional Optional<User> user = getUser(); String name = user.map(User::getName).orElse("Default User"); 💡 Benefits of using Optional: Reduces chances of NullPointerException Makes code more readable and expressive Encourages better null handling practices Using Optional in modern Java applications helps developers write safer and more maintainable code. #Java #JavaTips #SoftwareDevelopment #JavaDeveloper #Programming
To view or add a comment, sign in
-
📌 HttpClient in Java 11 – Finally, a Modern Way to Make HTTP Calls 🚀 If you're still using HttpURLConnection in Java… It’s time to upgrade. 👉 Introduced in Java 11 👉 Part of java.net.http package 👉 Modern replacement for HttpURLConnection And yes — it’s much cleaner. 🤯 The Old Way (HttpURLConnection) - Verbose - Hard to read - Manual stream handling - Not very intuitive Making a simple GET request felt complicated. 🚀 The Modern Way (HttpClient – Java 11) HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://example.com")) .GET() .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); Clean. Readable. Modern. 🔥 What Makes It Powerful? - Supports HTTP/2 - Built-in asynchronous calls - CompletableFuture support - WebSocket support - Cleaner API design 🧠 Async Example client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenAccept(System.out::println); No third-party libraries needed. #Java #Java11 #BackendDevelopment #SoftwareEngineering #InterviewPreparation
To view or add a comment, sign in
-
-
💡 Say Goodbye to NullPointerExceptions in Java! One of the most common issues Java developers face is the infamous NullPointerException — often called the “billion-dollar mistake.” With the evolution of Spring Boot 4 and Spring Framework 7, things are finally improving with compile-time null safety using JSpecify. By using annotations like @NullMarked and @Nullable, developers can now make nullability explicit in their code. This allows IDEs and tools to detect potential null issues much earlier. 🚀 Key advantages: • Catch potential NullPointerExceptions at compile time • Cleaner and more expressive APIs • Better IDE support and developer guidance • More stable and reliable production systems Instead of discovering null issues at runtime, we can now identify them during development itself. This is a big step toward writing safer and more maintainable Java applications. Exciting improvements ahead for the Java + Spring ecosystem! 🔥 #Java #SpringBoot #SpringFramework #BackendDevelopment #SoftwareEngineering
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development