0% found this document useful (0 votes)
51 views4 pages

WEEK-9 JAVA - KCPD

The document discusses three Java programs: 1) A program that creates three threads to display messages ("Good Morning", "Hello", "Welcome") at different time intervals (1, 2, 3 seconds respectively). 2) A program that implements the producer-consumer problem using inter-thread communication, linked lists, and synchronization. 3) A program that creates a GUI email registration form using AWT components like labels, text fields, checkboxes and buttons to collect user information.

Uploaded by

vnraids
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)
51 views4 pages

WEEK-9 JAVA - KCPD

The document discusses three Java programs: 1) A program that creates three threads to display messages ("Good Morning", "Hello", "Welcome") at different time intervals (1, 2, 3 seconds respectively). 2) A program that implements the producer-consumer problem using inter-thread communication, linked lists, and synchronization. 3) A program that creates a GUI email registration form using AWT components like labels, text fields, checkboxes and buttons to collect user information.

Uploaded by

vnraids
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
You are on page 1/ 4

WEEK 9:

a. Write a Java program that creates three threads. First thread displays —Good
Morning every one second, the second thread displays —Hello every two seconds
and the third thread displays —Welcome every three seconds.

public class ThreadExample {


public static void main(String[] args) {
// Creating three threads
Thread thread1 = new Thread(() -> displayMessage("Good Morning", 1000));
Thread thread2 = new Thread(() -> displayMessage("Hello", 2000));
Thread thread3 = new Thread(() -> displayMessage("Welcome", 3000));
// Starting the threads
thread1.start();
thread2.start();
thread3.start();
}
// Method to display a message at a given interval
private static void displayMessage(String message, long interval) {
while (true) {
System.out.println(message);
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
b. Write a Java program that correctly implements producer consumer problem using
the concept of inter thread communication.

import java.util.LinkedList;
public class ProducerConsumerExample {
public static void main(String[] args) {
final PC pc = new PC();
Thread producerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread consumerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
producerThread.start();
consumerThread.start();
try {
producerThread.join();
consumerThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class PC {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
while (list.size() == capacity) {
wait();
}
System.out.println("Producer produced-" + value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while (list.size() == 0) {
wait();
}
int val = list.removeFirst();
System.out.println("Consumer consumed-" + val);
notify();
Thread.sleep(1000);
}
}
}
}
}
c. Create a Email registration Form using Java AWT. The UI should have fields such
as name, address, sex, age, email, contact number, etc.

import java.awt.*;
import java.awt.event.*;
public class EmailRegistrationForm extends Frame {
Label nameLabel, addressLabel, sexLabel, ageLabel, emailLabel, contactLabel;
TextField nameField, addressField, ageField, emailField, contactField;
Checkbox maleCheckbox, femaleCheckbox;
Button submitButton;
public EmailRegistrationForm() {
super("Email Registration Form");
setSize(500, 300);
setLayout(null);
nameLabel = new Label("Name:");
nameLabel.setBounds(50, 50, 100, 20);
add(nameLabel);
nameField = new TextField();
nameField.setBounds(150, 50, 200, 20);
add(nameField);
addressLabel = new Label("Address:");
addressLabel.setBounds(50, 80, 100, 20);
add(addressLabel);
addressField = new TextField();
addressField.setBounds(150, 80, 200, 20);
add(addressField);
sexLabel = new Label("Sex:");
sexLabel.setBounds(50, 110, 100, 20);
add(sexLabel);
maleCheckbox = new Checkbox("Male");
maleCheckbox.setBounds(150, 110, 100, 20);
add(maleCheckbox);
femaleCheckbox = new Checkbox("Female");
femaleCheckbox.setBounds(250, 110, 100, 20);
add(femaleCheckbox);
ageLabel = new Label("Age:");
ageLabel.setBounds(50, 140, 100, 20);
add(ageLabel);
ageField = new TextField();
ageField.setBounds(150, 140, 200, 20);
add(ageField);
emailLabel = new Label("Email:");
emailLabel.setBounds(50, 170, 100, 20);
add(emailLabel);
emailField = new TextField();
emailField.setBounds(150, 170, 200, 20);
add(emailField);
contactLabel = new Label("Contact Number:");
contactLabel.setBounds(50, 200, 100, 20);
add(contactLabel);
contactField = new TextField();
contactField.setBounds(150, 200, 200, 20);
add(contactField);
submitButton = new Button("Submit");
submitButton.setBounds(200, 230, 100, 20);
add(submitButton);
setVisible(true);
}
public static void main(String[] args) {
EmailRegistrationForm form = new EmailRegistrationForm();
}
}

You might also like