0% found this document useful (0 votes)
15 views9 pages

Exercise 7

The document contains several Java programs demonstrating multithreading concepts, including creating threads using both the Thread class and Runnable interface, checking thread status with isAlive and join methods, implementing daemon threads, and solving the Producer-Consumer problem using synchronization. Each section provides code examples and explanations of how the threads operate and interact with each other. The output of each program illustrates the expected behavior of the threads in action.

Uploaded by

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

Exercise 7

The document contains several Java programs demonstrating multithreading concepts, including creating threads using both the Thread class and Runnable interface, checking thread status with isAlive and join methods, implementing daemon threads, and solving the Producer-Consumer problem using synchronization. Each section provides code examples and explanations of how the threads operate and interact with each other. The output of each program illustrates the expected behavior of the threads in action.

Uploaded by

Hemanth Atla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Exercise - 7

a) Write a JAVA program that creates threads by extending Thread class.First thread
display “Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds
and the third display “Welcome” every 3 seconds,(Repeat the same by implementing
Runnable)
class GoodMorningRunnable implements Runnable {
public void run() {
try {
while (true) {
System.out.println("Good Morning");
Thread.sleep(1000); // 1 second
}
} catch (InterruptedException e) {
System.out.println("Good Morning Runnable Interrupted");
}
}
}

class HelloRunnable implements Runnable {


public void run() {
try {
while (true) {
System.out.println("Hello");
Thread.sleep(2000); // 2 seconds
}
} catch (InterruptedException e) {
System.out.println("Hello Runnable Interrupted");
}
}
}
class WelcomeRunnable implements Runnable {
public void run() {
try {
while (true) {
System.out.println("Welcome");
Thread.sleep(3000); // 3 seconds
}
} catch (InterruptedException e) {
System.out.println("Welcome Runnable Interrupted");
}
}
}
public class RunnableExample {
public static void main(String[] args) {
Thread t1 = new Thread(new GoodMorningRunnable());
Thread t2 = new Thread(new HelloRunnable());
Thread t3 = new Thread(new WelcomeRunnable());

t1.start();
t2.start();
t3.start();
}
}

b) Write a program illustrating is Alive and join ()


class SimpleThread extends Thread {
private String name;

public SimpleThread(String name) {


this.name = name;
}

public void run() {


System.out.println(name + " starting.");
try {
Thread.sleep(1000); // Simulate work for 1 second
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " finished.");
}
}

public class SimpleThreadExample {


public static void main(String[] args) {
SimpleThread t1 = new SimpleThread("Thread 1");
SimpleThread t2 = new SimpleThread("Thread 2");

t1.start();
t2.start();

System.out.println("Is t1 alive? " + t1.isAlive());


System.out.println("Is t2 alive? " + t2.isAlive());

try {
t1.join(); // Main thread waits for t1 to finish
t2.join(); // Main thread waits for t2 to finish
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}

System.out.println("Is t1 alive after join? " + t1.isAlive());


System.out.println("Is t2 alive after join? " + t2.isAlive());
System.out.println("Main thread finished.");
}
}
OUTPUT:
Is t1 alive? true
Is t2 alive? true
Thread 2 starting.
Thread 1 starting.
Thread 1 finished.
Thread 2 finished.
Is t1 alive after join? false
Is t2 alive after join? false
Main thread finished.

c) Write a Program illustrating Daemon Threads


Daemon threads

Daemon threads are background threads that automatically terminate when all user threads
(non-daemon threads) finish executing.

In this example, we’ll create a daemon thread that runs continuously in the background,
while a main thread completes after a short time. When the main thread finishes, the daemon
thread also stops automatically.

class DaemonThread extends Thread {


public void run() {
while (true) {
System.out.println("Daemon thread running in background...");
try {
Thread.sleep(500); // Pauses for 500 ms
} catch (InterruptedException e) {
System.out.println("Daemon thread interrupted");
}
}
}
}

public class DaemonThreadExample {


public static void main(String[] args) {
DaemonThread daemonThread = new DaemonThread();
daemonThread.setDaemon(true); // Set this thread as daemon
daemonThread.start(); // Start the daemon thread

System.out.println("Main thread running...");


try {
Thread.sleep(2000); // Main thread runs for 2 seconds
} catch (InterruptedException e) {
System.out.println("Main thread interrupted");
}

System.out.println("Main thread finished. Daemon thread will stop automatically.");


}
}

Output:
Main thread running...
Daemon thread running in background...
Daemon thread running in background...
Daemon thread running in background...
Daemon thread running in background...
Main thread finished. Daemon thread will stop automatically.

d) Write a JAVA program Producer Consumer Problem

Producer-Consumer Problem to make it as straightforward as possible for students.

In this program:

 The Producer will produce a single item and place it in a simple buffer.
 The Consumer will take the item from the buffer.
 We’ll use wait() and notify() to manage when each can act on the buffer.

class Buffer {

private int item; // A place to hold a single item

private boolean hasItem = false; // True if the buffer has an item

// Method for the Producer to add an item

public synchronized void produce(int value) throws InterruptedException {

while (hasItem) { // If buffer already has an item, wait

wait();

item = value;

hasItem = true;

System.out.println("Produced: " + item);

notify(); // Notify the Consumer that there's a new item

// Method for the Consumer to consume an item

public synchronized int consume() throws InterruptedException {


while (!hasItem) { // If buffer is empty, wait

wait();

System.out.println("Consumed: " + item);

hasItem = false;

notify(); // Notify the Producer to produce again

return item;

// Producer class

class Producer extends Thread {

private Buffer buffer;

public Producer(Buffer buffer) {

this.buffer = buffer;

public void run() {

int count = 0;

try {

while (true) {

buffer.produce(count++); // Produce a new item

Thread.sleep(1000); // Pause to simulate production time

}
} catch (InterruptedException e) {

System.out.println("Producer interrupted");

// Consumer class

class Consumer extends Thread {

private Buffer buffer;

public Consumer(Buffer buffer) {

this.buffer = buffer;

public void run() {

try {

while (true) {

buffer.consume(); // Consume an item

Thread.sleep(1500); // Pause to simulate consumption time

} catch (InterruptedException e) {

System.out.println("Consumer interrupted");

}
public class SimpleProducerConsumerExample {

public static void main(String[] args) {

Buffer buffer = new Buffer(); // Create a shared buffer

Producer producer = new Producer(buffer);

Consumer consumer = new Consumer(buffer);

producer.start(); // Start the producer thread

consumer.start(); // Start the consumer thread

Output:

Produced: 0

Consumed: 0

Produced: 1

Consumed: 1

Produced: 2

Consumed: 2

...

You might also like