Ex No: 2.1 Programs Using Threads and Multi-Threading Date:: 717822D119 - ILAKKIYAN J 24
Ex No: 2.1 Programs Using Threads and Multi-Threading Date:: 717822D119 - ILAKKIYAN J 24
1
Programs using threads and multi-threading
Date:
Question
Write a Java program that creates two threads T1 and T2 by extending Thread class. T1 asks
the user to enter an integer and displays a statement that indicates whether the integer is even
or odd.T2 asks the user to enter three integers and displays them in ascending and descending
order.
Aim
To develop a java program to create 2 threads to perform certain functions.
Code
import java.util.Scanner;
class T1 extends Thread {
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Thread T1: Enter an integer:");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println("Thread T1: The entered number is even.");
} else {
System.out.println("Thread T1: The entered number is odd.");
}
}
}
class T2 extends Thread {
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Thread T2: Enter three integers:");
int[] nums = new int[3];
for (int i = 0; i < 3; i++) {
nums[i] = scanner.nextInt();
}
717822D119 - ILAKKIYAN J 24
System.out.print(num + " ");
}
System.out.println();
System.out.println("Thread T2: Descending order:");
for (int i = 2; i >= 0; i--) {
System.out.print(nums[i] + " ");
}
}
}
public class T1T2 {
public static void main(String[] args) {
T1 t1 = new T1();
T2 t2 = new T2();
t1.start();
t2.start();
}
}
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 25
Ex no: 2.2
Programs using threads and multi-threading
Date:
Question
Write a Java program that creates 100 threads by implementing Runnable interface. Each
thread adds 1 to a variable ‘sum’ that is initially 0. Display the value of sum after each
increment.
Aim
To develop a java program to create 100 threads and increment sum.
Code
public class ThreadIncrementExample {
public static void main(String[] args) {
SumHolder sumHolder = new SumHolder();
class SumHolder {
private int sum;
@Override
public void run() {
sumHolder.increment();
}
}
717822D119 - ILAKKIYAN J 26
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 27
Ex no: 2.3
Programs using threads and multi-threading
Date:
Question
Write a Java program that creates three threads to count the words in three files address.txt,
Homework.java and report.txt. and displays them in the following format:
address.txt: 1052
Homework.java: 445
report.txt: 2099
Aim
To develop a java program that create threads to get the word count from 3 different txt files.
Code
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
thread1.start();
thread2.start();
thread3.start();
try {
thread1.join();
thread2.join();
thread3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
717822D119 - ILAKKIYAN J 28
@Override
public void run() {
int wordCount = countWords(filename);
System.out.println(filename + ": " + wordCount);
}
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 29
Ex no: 2.4
Programs using threads and multi-threading
Date:
Question
Write a Java program in which multiple threads add and remove elements from a LinkedList.
Aim
To develop a java program that creates multiple threads to add or remove elements from
linkedlist.
Code
import java.util.LinkedList;
producer1.start();
producer2.start();
consumer1.start();
consumer2.start();
}
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
synchronized (list) {
list.add(i);
System.out.println("Producer " + id + " added: " + i);
list.notifyAll();
}
717822D119 - ILAKKIYAN J 30
try {
Thread.sleep((long) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
synchronized (list) {
while (list.isEmpty()) {
try {
list.wait(); // Wait for the producer to add elements
} catch (InterruptedException e) {
e.printStackTrace();
}
}
int value = list.removeFirst();
System.out.println("Consumer " + id + " removed: " + value);
}
try {
Thread.sleep((long) (Math.random() * 1000)); // Simulate some work
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
717822D119 - ILAKKIYAN J 31
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 32
Ex no: 2.5
Programs using threads and multi-threading
Date:
Question
Consider a banking scenario in which a customer wishes to withdraw or deposit an amount
from his/her account. Write a Java program which ensures that only one person is able to
perform withdraw or deposit on a bank account at a time. Illustrate the above scenario using
the synchronization concept in Java.
Aim
To develop java program that creates threads to withdraw or deposit in bank account.
Code
class BankAccount {
private double balance;
717822D119 - ILAKKIYAN J 33
@Override
public void run() {
if (isDeposit) {
account.deposit(amount);
} else {
account.withdraw(amount);
}
}
}
thread1.start();
thread2.start();
thread3.start();
}
}
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 34
Ex no: 2.6
Programs using threads and multi-threading
Date:
Question
Write a program for Inter Thread Communication process. Create three classes’ Consumer,
Producer and Stock.
• Stock class which contains synchronized getStock() and putStock() methods.
• Producer class invokes addStock() method.
• Consumer class invokes getStock() method
• Create a Main class that starts Producer and Consumer
Aim
To develop a java program to add and remove stocks using threads.
Code
class Stock {
private int stock = 0;
private boolean available = false;
717822D119 - ILAKKIYAN J 35
public synchronized int getStock() {
while (!available) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
available = false;
System.out.println("Consumer consumed: " + stock);
notify();
return stock;
}
}
717822D119 - ILAKKIYAN J 36
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
producer.start();
consumer.start();
}
}
Output
Result
Thus, the Java program for the given condition has been successfully developed and
the output was verified.
717822D119 - ILAKKIYAN J 37