2079 4

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]

Advanced Java ProgrammingChapter 1: Programming in JavaConcurrency

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 calls run()
  • 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

FeatureExtending ThreadImplementing Runnable
InheritanceCannot extend other classCan extend another class
FlexibilityLess flexibleMore flexible
PreferredSimple programsReal-world programs

Thread Life Cycle

A thread passes through the following states:

  1. New - Thread object is created
  2. Runnable - start() is called
  3. Running - Thread is executing
  4. Blocked/Waiting - Thread is waiting for a resource
  5. 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.