Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Multiple Threads
What is Multiple Threads?
Why Use Multiple Threads? How to Thread Creation in Java Thread Life Cycle Advantages of Multiple Threads Challenges of Multiple Threads What is Multiple Threads? Is refer to the concurrent execution of two or more threads in a program. A thread is the smallest unit of a CPU's execution A program can have multiple threads running at the same time Is a technique in which multiple threads are used to perform different tasks in parallel within a single program. Why Use Multiple Threads Improved Performance and Responsiveness:- By using multiple threads, programs can perform multiple operations concurrently, which improves efficiency and responsiveness. Efficient CPU Utilization:- multiple threads can run simultaneously on different cores. This leads to better CPU utilization Concurrency in Applications:-Multithreading is useful in applications that need to handle several tasks at once, Thread Creation in Java 1) Using the Thread Class:-In Java, a thread can be created by subclassing the Thread class and overriding its run() method. example: class MyThread extends Thread { public void run() { System.out.println("Thread is running"); } } MyThread t = new MyThread(); t.start(); 2) Using the Runnable Interface:- This allows the class to implement the run() method, and then pass an instance of the class to a Thread object. Example: class MyRunnable implements Runnable { public void run() { System.out.println("Thread is running"); } } MyRunnable r = new MyRunnable(); Thread t = new Thread(r); t.start(); Thread Life Cycle New:- Thread object created. Runnable:- Ready to run but waiting for CPU. Running:-Actively executing. Blocked/Waiting:- Waiting for a resource. Terminated:- Completed execution. Advantages of Multiple Threads Improved Program Performance:- programs can be executed more efficiently, especially on multi-core processors. Better Responsiveness:-multithreading ensures that the program remains responsive by delegating time-consuming operations to separate threads Resource Sharing:-Threads within the same program share the same memory and resources, such as variables and files, Asynchronous Execution:-Multiple threads can be used to perform tasks asynchronously, Simplifies Complex Problems:Problems like simulations, real-time event handling, and parallel processing of large datasets are easier to implement with thread Challenges of Multiple Threads Race Conditions:-When multiple threads access shared resources simultaneously without proper synchronization, Deadlock:-when two or more threads are blocked indefinitely, waiting for each other to release resources. Thread Management:-Managing multiple threads can be complex, especially in applications with a large number of threads. Context Switching:-When multiple threads share the CPU, the operating system may need to frequently switch between threads, Difficulty in Debugging:-Multithreaded programs are harder to debug because of timing issues, non-deterministic behavior, and race conditions. Conclusion Multi-threading boosts performance and resource utilization. Java simplifies thread management with Thread and Runnable. Effective for real-world applications like banking, gaming, and data processing Group Member NAME ID 1) ABIDI ALEMU RU1163/15 2) DANIEL BIRHANU RU0585/15 3) GEMECHU BONSA RU0913/15 4) HOJIWAK YOHANIS RU1085/15 5) KEDIR MUNDINO RU1204/15 6) MULUGETA TESFAYE RU1550/15 7) SEGNI ABERA RU1829/15 8) GUTU ITANA RU0957/15