CSC419 ยท 2079 ยท 5 marks
What is multithreading? How can you create multithreaded program in Java? Explain.
What is multithreading? How can you create multithreaded program in Java? Explain. [5]
Quick answer
Multithreading is a feature of Java that allows concurrent execution of two or more parts (threads) of a program to maximize CPU utilization. A thread is a lightweight process that has its own files and stacks but can access shared data from other threads within the same process.
Multithreading in Java
What is Multithreading?
The main advantages of multithreading are:
- Provides simultaneous execution of two or more parts of a program
- Shares the same memory, making it more efficient than separate processes
- Maximizes CPU time utilization
Creating a Multithreaded Program in Java
There are two ways to create a thread in Java:
Method 1: Extending the Thread Class
A class extends the built-in Thread class and overrides its run() method.
class MyThread extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread: " + i);
}
}
}
public class MultiThreadDemo {
public static void main(String[] args) {
MyThread t1 = new MyThread(); // Create thread object
MyThread t2 = new MyThread(); // Create another thread object
t1.start(); // Start thread 1 (calls run() internally)
t2.start(); // Start thread 2
}
}
Key Points:
start()method is called to begin execution; it internally callsrun()- Do not call
run()directly, as it will not create a new thread
Method 2: Implementing the Runnable Interface
A class implements the Runnable interface and provides the body of the run() method. This is preferred when the class already extends another class (since Java does not support multiple inheritance).
class MyRunnable implements Runnable {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println("Runnable Thread: " + i);
}
}
}
public class MultiThreadDemo2 {
public static void main(String[] args) {
MyRunnable obj = new MyRunnable();
Thread t1 = new Thread(obj); // Pass Runnable object to Thread
Thread t2 = new Thread(obj);
t1.start();
t2.start();
}
}
Comparison of Both Methods
| Feature | Extending Thread | Implementing Runnable |
|---|---|---|
| Inheritance | Cannot extend other class | Can extend another class |
| Flexibility | Less flexible | More flexible |
| Preferred | Simple programs | Real-world programs |
Thread Life Cycle
A thread passes through the following states:
- New - Thread object is created
- Runnable -
start()is called - Running - Thread is executing
- Blocked/Waiting - Thread is waiting for a resource
- Terminated - Thread finishes execution
Summary
Multithreading in Java enables multiple tasks to run simultaneously, improving performance and CPU utilization. It can be implemented either by extending the Thread class or implementing the Runnable interface, with Runnable being the more flexible and commonly preferred approach.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.