Multitasking And Multitasking In java
1. What is Multitasking?
• Multitasking refers to executing multiple tasks or processes simultaneously. There are two types:
• Process-based multitasking: Running multiple independent processes (e.g., Word processor, browser).
• Thread-based multitasking: Multiple threads of the same process running concurrently.
2. What is Multithreading in Java?
• Multithreading allows multiple threads (lightweight subprocesses) to run concurrently within a single program. Threads share the same memory space and resources but execute independently, improving efficiency in tasks like file I/O, calculations, or network operations.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String[] args) {
MyThread thread1 = new MyThread();
thread1.start();
}
}3. Pros and Cons of Multithreading:
Pros:
• Increased performance: Tasks can execute concurrently, improving CPU utilization.
• Resource sharing: Threads share memory space, which is more efficient than creating multiple processes.
• Responsive applications: User interfaces can remain responsive by offloading time-consuming tasks to separate threads.
Cons:
• Complexity: Multithreading introduces challenges like thread synchronization, debugging, and managing shared resources.
• Overhead: Improper use can lead to resource contention, memory leaks, and performance degradation.
4. What Else is Used in Java Multithreading?
• Thread Pooling: Efficiently manage a fixed number of reusable threads (ExecutorService).
Get NextGenML’s stories in your inbox
Join Medium for free to get updates from this writer.
• Synchronization: Control access to shared resources to avoid thread interference.
• Locks (Lock and ReentrantLock): For more advanced control over synchronization than the synchronized keyword.
• Concurrency Utilities: Java provides utilities (java.util.concurrent) like CountDownLatch, Semaphore, and ConcurrentHashMap to simplify thread management.
5. When to Use Multithreading?
• Use multithreading when tasks can run independently or need to perform I/O operations concurrently, such as:
• Parallel tasks (e.g., file reading and data processing).
• Asynchronous processing (e.g., handling user input while performing background tasks).
• CPU-intensive operations (e.g., calculations distributed across multiple threads).
6. What is a Deadlock?
• Deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a resource. This happens when threads acquire multiple locks in different orders, leading to a circular wait.
Example of Deadlock:
public class DeadlockExample {
public static void main(String[] args) {
final Object lock1 = new Object();
final Object lock2 = new Object();
Thread t1 = new Thread(() -> {
synchronized (lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
synchronized (lock2) {
System.out.println("Thread 1: Holding lock 2...");
}
}
});
Thread t2 = new Thread(() -> {
synchronized (lock2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); } catch (InterruptedException e) {}
synchronized (lock1) {
System.out.println("Thread 2: Holding lock 1...");
}
}
});
t1.start();
t2.start();
}
}In this example, thread t1 acquires lock1 and waits for lock2, while thread t2 acquires lock2 and waits for lock1, causing a deadlock.
Summary:
• Multitasking: Running multiple tasks simultaneously.
• Multithreading in Java: Lightweight, concurrent execution of threads within a process to optimize performance and resource usage.
• Pros: Improved performance, efficient resource use, responsive apps.
• Cons: Complexity, potential for issues like deadlocks.
• When to Use: For CPU-bound or I/O-bound tasks that can run independently.
• Deadlock: A situation where threads block each other while waiting for resources, causing the system to freeze.









