Giter Site home page Giter Site logo

shubh2-0 / runnable_vs._callable Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 0.0 54 KB

This GitHub repository explores Runnable and Callable interfaces in Java. It provides examples and code implementations to demonstrate their usage in concurrent programming. Learn the differences and when to use each interface. Enhance your understanding of these Java interfaces.

Java 100.00%
callable interfaces java java-8 multi-threading runnable threading

runnable_vs._callable's Introduction

Runnable Vs Callable ๐Ÿคœ๐Ÿค›

This GitHub repository contains three projects that demonstrate the implementation and usage of Runnable and Callable interfaces in Java.

Since Java's early days, multithreading has been a major aspect of the language. Runnable is the core interface provided for representing multithreaded tasks, and Java 1.5 provided Callable as an improved version of Runnable.

What is Runnable?

Runnable is an interface that classes implementing it are going to be executed in threads. Here, you can see the Runnable interface. All your logic that needs to be executed in a thread will be in the overridden run method. You will notice that it is a void method.

Example :-

   public class MyRunnable implements Runnable {

    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i+" ");
        }
    }
    public static void main(String[] args) {
        Runnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();
     }
  }

What is Callable?

Everything I wrote about Runnable is valid for the Callable interface except one thing, return type. The method call will return any type after it completes its execution. Example :-

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable<Integer> {
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += i;
        }
        return sum;
    }
    public static void main(String[] args) throws Exception {
        Callable<Integer> myCallable = new MyCallable();
        FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println(futureTask.get());
    }
}

Execution Mechanism โš’๏ธ

Both interfaces are designed to represent a task that can be run by multiple threads. We can run Runnable tasks using the Thread class or ExecutorService, whereas we can only run Callables using the latter.

Return Values

1๏ธโƒฃ The Runnable interface is a functional interface and has a single run() method that doesn't accept any parameters or return any values.
2๏ธโƒฃ The Callable interface is a generic interface containing a single call() method that returns a generic value V:

Getting Started

๐Ÿš€ To run any of the projects in this repository, follow the instructions in their respective directories. Each project contains a README file with detailed explanations and steps to execute the code.

Requirements

โš™๏ธ To compile and execute the projects, ensure you have the following prerequisites:

  • Java Development Kit (JDK) 8 or higher
  • An integrated development environment (IDE) or a command-line interface (CLI) to build and run Java code

Contributing

๐Ÿค Contributions to this repository are welcome. If you find any issues or want to enhance the existing projects, feel free to open a pull request. Please make sure to follow the repository's guidelines and code of conduct.

Acknowledgements

๐Ÿ™ This repository was created to provide a simple and practical demonstration of the Runnable and Callable interfaces. It draws inspiration from various Java programming resources and examples available online. Special thanks to the contributors and developers of these resources for sharing their knowledge.

If you have any questions or suggestions, please feel free to reach out by creating an issue in this repository. Enjoy exploring the Runnable and Callable interfaces!

runnable_vs._callable's People

Contributors

shubh2-0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.