0% found this document useful (0 votes)
8 views12 pages

Java Multi-threading Examples and ArrayList Operations

Uploaded by

harshajoddd
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)
8 views12 pages

Java Multi-threading Examples and ArrayList Operations

Uploaded by

harshajoddd
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

PRACTICAL 23

AIM: Write a java program for producer and consumer problems using Threads.

class Buffer {
private int data;
private boolean available = false;

public synchronized void put(int value) {


while (available) {
try {
wait();
} catch (InterruptedException e) {
[Link]();
}
}
data = value;
available = true;
notify();
}

public synchronized int get() {


while (!available) {
try {
wait();
} catch (InterruptedException e) {
[Link]();
}
}
available = false;
notify();
return data;
}
}

class Producer extends Thread {


private Buffer buffer;
public Producer(Buffer buffer) {
[Link] = buffer;
}

@Override
public void run() {
for (int i = 0; i < 5; i++) {
[Link](i);
[Link]("Produced: " + i);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}

class Consumer extends Thread {


private Buffer buffer;

public Consumer(Buffer buffer) {


[Link] = buffer;
}

@Override
public void run() {
for (int i = 0; i < 5; i++) {
int data = [Link]();
[Link]("Consumed: " + data);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}

public class ThreadExample {


public static void main(String[] args) {
Buffer buffer = new Buffer();
Producer producer = new Producer(buffer);
Consumer consumer = new Consumer(buffer);

[Link]();
[Link]();
}
}

Output:

Produced: 0
Consumed: 0
Consumed: 1
Produced: 1
Produced: 2
Consumed: 2
Consumed: 3
Produced: 3
Consumed: 4
Produced: 4

Practical No: 24
AIM: Write a java program that implements a multi-thread application that has
three threads. First thread generates a random integer every 1 second and if the
value is even, the second thread computes the square of the number and prints. If
the value is odd, the third thread will print the value of the cube of the number.
import [Link];

class GeneratorThread extends Thread {


private Random random;
private EvenThread evenThread;
private OddThread oddThread;

public GeneratorThread(EvenThread evenThread, OddThread


oddThread) {
[Link] = new Random();
[Link] = evenThread;
[Link] = oddThread;
}

@Override
public void run() {
while (true) {
int number = [Link](100);
[Link]("Generated number: " + number);
if (number % 2 == 0) {
[Link](number);
} else {
[Link](number);
}
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]();
}
}
}
}

class EvenThread extends Thread {


private int number;

@Override
public void run() {
while (true) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
[Link]();
}
if (number != 0) {
[Link]("Square of " + number + "
is: " + number * number);
number = 0;
}
[Link](); // notify should be called on the
current object
}
}
}

public void setNumber(int number) {


synchronized (this) { // synchronize on this object
[Link] = number;
[Link](); // notify should be called on the
current object
}
}
}

class OddThread extends Thread {


private int number;

@Override
public void run() {
while (true) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
[Link]();
}
if (number != 0) {
[Link]("Cube of " + number + "
is: " + number * number * number);
number = 0;
}
[Link](); // notify should be called on the
current object
}
}
}

public void setNumber(int number) {


synchronized (this) { // synchronize on this object
[Link] = number;
[Link](); // notify should be called on the
current object
}
}
}

public class MultiThreadedApplication {


public static void main(String[] args) {
EvenThread evenThread = new EvenThread();
OddThread oddThread = new OddThread();
GeneratorThread generatorThread = new
GeneratorThread(evenThread, oddThread);

[Link]();
[Link]();
[Link]();
}
}

Output:

Generated number: 4
Square of 4 is: 16
Generated number: 98
Square of 98 is: 9604
Generated number: 31
Cube of 31 is: 29791
Generated number: 87
Cube of 87 is: 658503

……….. Etc.
Practical No: 25
AIM: write a program to create a dynamic array using the ArrayList class and
then print the contents of the array object.

import [Link];

public class DynamicArrayExample {


public static void main(String[] args) {
// Create an ArrayList (Dynamic Array)
ArrayList<Integer> dynamicArray = new ArrayList<>();

// Add elements to the ArrayList


[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);

// Print the contents of the ArrayList


[Link]("Contents of the dynamic array:");
for (int element : dynamicArray) {
[Link](element);
}
}
}

Output:

Contents of the dynamic array:


10
20
30
40
50

Process finished with exit code 0

Practical No: 26

AIM: Write programs to implement add, search and remove operations on ArrayList
objects.

import [Link];
import [Link];

public class ArrayListOperations {


private ArrayList<Integer> arrayList = new ArrayList<>();

public void addElement(int element) {


[Link](element);
[Link]("Element " + element + " added to the
ArrayList.");
}

public boolean searchElement(int element) {


return [Link](element);
}

public void removeElement(int element) {


if ([Link](element)) {
[Link]((Integer) element);
[Link]("Element " + element + " removed
from the ArrayList.");
} else {
[Link]("Element " + element + " not found
in the ArrayList.");
}
}

public void displayArrayList() {


[Link]("ArrayList: " + arrayList);
}

public static void main(String[] args) {


ArrayListOperations arrayListOperations = new
ArrayListOperations();
Scanner scanner = new Scanner([Link]);

while (true) {
[Link]("1. Add element");
[Link]("2. Search element");
[Link]("3. Remove element");
[Link]("4. Display ArrayList");
[Link]("5. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();

switch (choice) {
case 1:
[Link]("Enter element to add: ");
int addElement = [Link]();
[Link](addElement);
break;
case 2:
[Link]("Enter element to search:
");
int searchElement = [Link]();
boolean found =
[Link](searchElement);
if (found) {
[Link]("Element " +
searchElement + " found in the ArrayList.");
} else {
[Link]("Element " +
searchElement + " not found in the ArrayList.");
}
break;
case 3:
[Link]("Enter element to remove:
");
int removeElement = [Link]();

[Link](removeElement);
break;
case 4:
[Link]();
break;
case 5:
[Link](0);
break;
default:
[Link]("Invalid choice. Please
try again.");
}
}
}
}

1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit
Enter your choice: 1
Enter element to add: 50
Element 50 added to the ArrayList.

1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit

Enter your choice: 1


Enter element to add: 70
Element 70 added to the ArrayList.

1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit

1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit

Enter your choice: 3


Enter element to remove: 70
Element 70 removed from the ArrayList.

1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit

Enter your choice: 4


ArrayList: [50]
1. Add element
2. Search element
3. Remove element
4. Display ArrayList
5. Exit
Enter your choice: 5

Process finished with exit code 0

You might also like