0% found this document useful (0 votes)
4 views5 pages

Java OOPs: User-Defined Packages & Threads

Uploaded by

sampath oruganti
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)
4 views5 pages

Java OOPs: User-Defined Packages & Threads

Uploaded by

sampath oruganti
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

2-BTECH-CSE:: I SEM

OOPS THROUGH JAVA PROGRAMMING LAB


EXPERIMENT: 8
8(A): WRITE A JAVA PROGRAM TO CREATE AND USE A USER-DEFINED
PACKAGE IN JAVA:

Here's an example of how to create and use a user-defined package in Java:

Let's say we want to create a package named "example" and include a class called
"Calculator" within it.

Create a new directory structure for your package. create a folder named " "example".

Create the "[Link]" file and place it inside the "example" folder.

example/[Link]

package example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
if (b != 0) {
return a / b;
} else {
throw new ArithmeticException("Cannot divide by zero
!");
}
}
}

Now, create another file outside the "EXAMPLE" folder to access the Calculator class from
the user-defined package.

[Link]

import [Link];
public class PackageExample {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = [Link](5, 3);
[Link]("Addition: " + result);
result = [Link](5, 3);
[Link]("Subtraction: " + result);
result = [Link](5, 3);
[Link]("Multiplication: " + result);
result = [Link](10, 2);
[Link]("Division: " + result);
}
}
Output:

Addition: 8
Subtraction: 2
Multiplication: 15
Division: 5
8(B) : Write a JAVA program Producer Consumer Problem

What is the Producer-Consumer Problem?


The producer-consumer problem is a synchronization problem between different processes.
There are three entities in this problem: a producer, a consumer, and a memory buffer. Both
the producer and consumer share the same memory buffer.

The producer produces some items and pushes them into the memory buffer. A consumer
then consumes these items by popping them out of the buffer. If the buffer is empty, then the
consumer waits for the producer to push an item, which it consumes after the producer pushes
it.

The memory buffer is of fixed size. If it is full, the producer waits for the consumer to consume
an item before pushing a new one. The producer and consumer cannot access the buffer at
the same time – that is, it's mutually exclusive. Each process should wait for the other to finish
its work on the buffer before it can access the buffer.

Operating systems often encounter this problem where multiple processes access the same
memory space to perform their tasks.
import [Link]; public static class PC {
LinkedList<Integer> list = new LinkedList<>();
public class Threadexample int capacity = 2;
{ public void produce() throws
public static void main(String[] args) InterruptedException
throws InterruptedException {
{ int value = 0;
final PC pc = new PC(); while (true) {
Thread t1 = new Thread(new Runnable() { synchronized (this)
public void run() {
{ while ([Link]() == capacity)
try { wait();
[Link]();
} [Link]("Producer produced-"
catch (InterruptedException e) { + value);
[Link]();
} [Link](value++);
}
}); notify();

// Create consumer thread [Link](1000);


Thread t2 = new Thread(new Runnable() { } } }
public void run()
{ // Function called by consumer thread
try { public void consume() throws
[Link](); InterruptedException
} {
catch (InterruptedException e) { while (true) {
[Link](); synchronized (this)
} {
} while ([Link]() == 0)
}); wait();
int val = [Link]();
// Start both threads [Link]("Consumer consumed-
[Link](); + val);
[Link](); notify();
[Link](1000);
[Link](); }
[Link](); } } } }
} OUTPUT:
// This class has a list, producer (adds items to list Producer produced-0
// and consumer (removes items).
Producer produced-1
Consumer consumed-0
Consumer consumed-1
Producer produced-2

You might also like