How to call a method with a separate thread in Java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Calling a method with a separate thread in Java is a fundamental concept linked to parallel processing. Leveraging threads allows programs to perform multiple tasks concurrently, which can enhance the performance of applications significantly, especially those with computationally intensive operations or those that wait for some resources like file I/O or network operations. This article explores the fundamental concepts, demonstrates how to implement threading in Java, and provides examples to solidify understanding.
Understanding Threads in Java
Java provides built-in support for multithreaded programming through its java.lang.Thread class and the java.util.concurrent package. A thread in Java is essentially a lightweight subprocess; the smallest unit of processing. In Java, threading can be achieved primarily by two methods:
- Extending the
ThreadClass: Subclassing theThreadclass and overriding itsrunmethod. - Implementing the
RunnableInterface: Implementing theRunnableinterface because Java does not support multiple inheritance, making this approach more flexible and widely used.
Creating a Thread by Extending the Thread Class
When you extend the Thread class, you create a thread by defining a new class and overriding the run method. Here's a technical explanation along with an example:
Key Points:
- Subclass the Thread Class: Create a class that extends
Thread. - Override the run() Method: Override the
runmethod to insert the code that should be executed by the thread. - Invoke start(): Call the
startmethod on the thread object to initiate a new thread of execution.
Creating a Thread by Implementing the Runnable Interface
To achieve more flexibility, implement the Runnable interface. This method is preferred as it allows other inheritance in your class.
Key Points:
- Implement the Runnable Interface: Create a class that implements
Runnable. - Override the run() Method: Implement the
runmethod with the code for the task. - Pass Runnable to Thread: Create an instance of
Thread, passing it theRunnableimplementation, and callstartto execute the thread.
Advantages of Using Runnable Over Thread
- Decoupled Design: Allows implementing the
Runnableinterface and extending another base class. - Reusability: The
Runnablecan be executed by several threads, making object design cleaner.
Using the Executor Framework
For better scalability and management of threads, Java introduced the Executor framework which provides thread pool functionality.
Key Points:
- Fixed Thread Pool: Allows running predetermined maximum threads concurrently. Consider using
Executors.newFixedThreadPool(). - Submit Tasks:
submitmethod places tasks into the thread pool for execution. - Graceful Shutdown:
shutdown()ensures orderly termination, executing previously submitted tasks.
Table Summarizing Key Points
| Aspect | Thread Class | Runnable Interface | Executor Framework |
| Extends or Implements | Extends Thread | Implements Runnable | Used with Callable & Runnable |
| Inheritance Usage | Not possible to extend another class | Allows to extend other classes | Not applicable |
| Ease of Thread Management | Manual thread management | Manual thread management | Managed by Executors |
| Reuse of Runnable Objects | Not reusable | Reusable | Highly reusable in a thread pool |
| Thread Handling | Calls start() on Thread | Pass to Thread and then call start() | Uses submit() to execute threads |
Conclusion
Calling a method in a separate thread in Java enables a high degree of parallelism in applications. By understanding the different approaches—extending Thread, implementing Runnable, or leveraging the Executor framework—you can choose the best method to suit your application requirements, balancing simplicity with powerful features for efficient thread management. This flexibility in Java enhances both performance and responsiveness in applications, essential in modern software development.
With this knowledge, one can more effectively design multithreaded applications to leverage multi-core processors and provide a smoother, more responsive user experience.

