0% found this document useful (0 votes)
18 views6 pages

Java Asnmt 2

The document is an assignment for a Java programming course at the Bangalore Institute of Technology, focusing on multithreaded programming. It outlines the creation of two classes, NumberPrinter and AlphabetPrinter, that execute concurrently, along with instructions for checking thread status and handling exceptions. The assignment also includes observations regarding thread behavior and output interleaving.

Uploaded by

uzmasuma123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Java Asnmt 2

The document is an assignment for a Java programming course at the Bangalore Institute of Technology, focusing on multithreaded programming. It outlines the creation of two classes, NumberPrinter and AlphabetPrinter, that execute concurrently, along with instructions for checking thread status and handling exceptions. The assignment also includes observations regarding thread behavior and output interleaving.

Uploaded by

uzmasuma123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING

BANGALORE INSTITUTE OF TECHNOLOGY


(An Autonomous Institution under VTU K.R. Road, V.V. Pura, Bengaluru-560 004)

Academic Year: 2025 – 2026

OBJECT ORIENTED PROGRAMMING WITH JAVA


(BCS306A)

“Assignment 2: Multithreaded Programming in Java”

Submitted By

STUDENT USN STUDENT NAME


“1BI24AI012” “BHAGYASHREE MUDAKAN”

Under the guidance of


Prof. Nagabushan P
Assistant Professor
Department of AI&ML, BIT
Bengaluru-560004
Assignment 2

“Write a Java program to simulate two independent tasks running


concurrently:

1. Create a class ‘NumberPrinter' that extends Thread and overrides


run().
- It should print numbers from 1 to 5 with a delay of 500ms between
each using [Link](500).
- Print which thread is executing : "NumberPrinter:1" ,
"NumberPrinter: 2", etc.

2. Create a class AlphabetPrinter that implements Runnable.


- It should print letters A to E with a 500ms delay between each.
- Use [Link](500) and handle InterruptedException.

3. In the main method:


d) Start both threads.
b) Use isAlive() to check the status of both threads before and after
completion.
c) Use join() so that the main thread waits until both finish.
d) After both complete, print "All tasks completed".

4. Observe and write in comments:


- Does output appear interleaved? Why?
- What happens if you remove join()?
- When does isAlive() return false?

Note : Use only simple Java syntax. Do not use lambda expressions or
collections.”
SOURCE CODE :-

class NumberPrinter extends Thread {


public void run() {
try {
for (int i=1 ; i<=5; i++) {
[Link]("NumberPrinter: " + i);
[Link](500);
}
} catch (InterruptedException e) {
[Link]("NumberPrinter interrupted");
}
}
}

class AlphabetPrinter extends Thread {


public void run() {
try {
for (char c = 'A'; c <= 'E'; c++) {
[Link]("AlphabetPrinter: " + c);
[Link](500);
}
} catch (InterruptedException e) {
[Link]("AlphabetPrinter interrupted");
}
}
}

public class MultiThreadDemo {


public static void main(String[] args) {

NumberPrinter t1 = new NumberPrinter();


AlphabetPrinter t2 = new AlphabetPrinter();

// Check thread status before start


[Link]("Before start:");
[Link]("t1 alive: " + [Link]());
[Link]("t2 alive: " + [Link]());

// Start threads
[Link]();
[Link]();
try {
// Wait for both threads to finish
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread interrupted");
}

// Check thread status after completion


[Link]("After completion:");
[Link]("t1 alive: " + [Link]());
[Link]("t2 alive: " + [Link]());

[Link]("All tasks completed");


}
}
OUTPUT:
Observations & Answers :-

1. Do outputs appear interleaved?


Yes, the outputs of both threads appear interleaved because they run
concurrently.

2. What happens if you remove join()?


The main thread may terminate before the child threads finish
execution, and "All tasks completed" may appear early.

3. What happens if you remove sleep()?


Threads will execute very fast, and outputs may appear grouped
instead of interleaved.

4. What happens if you remove isAlive()?


The program will still run correctly, but thread status information
will not be displayed.

You might also like