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
[Link]();
[Link]();
[Link]();
}
// Method to display a message at a given interval
private static void displayMessage(String message, long interval) {
while (true) {
[Link](message);
try {
[Link](interval);
} catch (InterruptedException e) {
[Link]();
}
}
}
}
b. Write a Java program that correctly implements producer consumer problem using
the concept of inter thread communication.
import [Link];
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 {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
}
});
Thread consumerThread = new Thread(new Runnable() {
@Override
public void run() {
try {
[Link]();
} catch (InterruptedException e) {
[Link]();
}
}
});
[Link]();
[Link]();
try {
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]();
}
}
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 ([Link]() == capacity) {
wait();
}
[Link]("Producer produced-" + value);
[Link](value++);
notify();
[Link](1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while ([Link]() == 0) {
wait();
}
int val = [Link]();
[Link]("Consumer consumed-" + val);
notify();
[Link](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 [Link].*;
import [Link].*;
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:");
[Link](50, 50, 100, 20);
add(nameLabel);
nameField = new TextField();
[Link](150, 50, 200, 20);
add(nameField);
addressLabel = new Label("Address:");
[Link](50, 80, 100, 20);
add(addressLabel);
addressField = new TextField();
[Link](150, 80, 200, 20);
add(addressField);
sexLabel = new Label("Sex:");
[Link](50, 110, 100, 20);
add(sexLabel);
maleCheckbox = new Checkbox("Male");
[Link](150, 110, 100, 20);
add(maleCheckbox);
femaleCheckbox = new Checkbox("Female");
[Link](250, 110, 100, 20);
add(femaleCheckbox);
ageLabel = new Label("Age:");
[Link](50, 140, 100, 20);
add(ageLabel);
ageField = new TextField();
[Link](150, 140, 200, 20);
add(ageField);
emailLabel = new Label("Email:");
[Link](50, 170, 100, 20);
add(emailLabel);
emailField = new TextField();
[Link](150, 170, 200, 20);
add(emailField);
contactLabel = new Label("Contact Number:");
[Link](50, 200, 100, 20);
add(contactLabel);
contactField = new TextField();
[Link](150, 200, 200, 20);
add(contactField);
submitButton = new Button("Submit");
[Link](200, 230, 100, 20);
add(submitButton);
setVisible(true);
}
public static void main(String[] args) {
EmailRegistrationForm form = new EmailRegistrationForm();
}
}