Chapter 1 Lab Exercises
//Creating thread by extending Thread class
class MyThread extends Thread {
public void run() {
[Link]("Thread is running...");
}}
public class ThreadExample {
public static void main(String[] args) {
MyThread t1 = new MyThread();
[Link](); // Starts a new thread
}}
//Creating thread by implementing Runnable interface
class MyRunnable implements Runnable {
public void run() {
[Link]("Thread is running using Runnable...");
}}
public class RunnableExample {
public static void main(String[] args) {
MyRunnable obj = new MyRunnable();
Thread t = new Thread(obj);
[Link]();
}}
//Running both threads
class ThreadA extends Thread {
public void run() {
for(int i=1; i<=3; i++)
[Link]("From ThreadA: " + i);
}}
1
class ThreadB implements Runnable {
public void run() {
for(int i=1; i<=3; i++)
[Link]("From ThreadB: " + i);
}}
public class TestThreads {
public static void main(String[] args) {
ThreadA t1 = new ThreadA();
//Thread t2 = new Thread(new ThreadB());
ThreadB obj=new ThreadB();
Thread t2=new Thread(obj);
[Link]();
[Link]();
}}
Note: Output (order may vary due to concurrent execution):
//Thread methods Demonstration
class ThreadMethod extends Thread{
public void run(){
[Link]("Thread runs on "+[Link]().getName());
}}
public class ThreadMethods{
public static void main(String[] args) throws InterruptedException{
ThreadMethod t1 =new ThreadMethod();
[Link]("Thread 1");
2
ThreadMethod t2=new ThreadMethod();
[Link]("Thread 2");
ThreadMethod t3=new ThreadMethod();
[Link]("Thread 3");
[Link]("before start "+[Link]());
[Link]("Thread 1 priority: "+[Link]());
[Link]();
[Link]("After start "+[Link]());
[Link]();
[Link]("After interuption "+[Link]());
//[Link](Thread.MAX_PRIORITY);
[Link]();
[Link]();
}}
//Thread Synchronization in Java
class Bakery {
boolean breadReady = false;
synchronized void bakeBread() {
breadReady = true;
[Link]("Baker: Bread is ready!");
notify(); // Wake up the seller
}
3
synchronized void sellBread() {
while (!breadReady) {
try {
wait(); // Wait until baker notifies
} catch (Exception e) {
[Link]();
}}
[Link]("Seller: Selling the bread!");
}}
public class MessagingDemo {
public static void main(String[] args) {
final Bakery bakery = new Bakery();
Thread seller = new Thread(new Runnable() {
public void run() {
[Link]();
}});
Thread baker = new Thread(new Runnable() {
public void run() {
[Link]();
}
});
[Link]();
[Link]();
}}
//Online Food Delivery System Using Multithreading
class OrderProcessing extends Thread {
4
public void run() {
[Link]("Processing order...");
try { [Link](3000); } catch (InterruptedException e) {}
[Link]("Order processed successfully!");
}}
class PaymentProcessing extends Thread {
public void run() {
[Link]("Processing payment...");
try { [Link](2000); } catch (InterruptedException e) {}
[Link]("Payment completed!");
}}
class NotificationService extends Thread {
public void run() {
[Link]("Sending notifications...");
try { [Link](1000); } catch (InterruptedException e) {}
[Link]("Notification sent to driver!");
}}
public class FoodDeliveryApp {
public static void main(String[] args) {
OrderProcessing order = new OrderProcessing();
PaymentProcessing payment = new PaymentProcessing();
NotificationService notify = new NotificationService();
[Link]();
[Link]();
[Link]();
}}
//Producer-consumer problem
//Without synchronization
5
class SharedData {
int value;
}
class Producer extends Thread {
SharedData data;
Producer(SharedData d) {
data = d;
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link] = i; // produce data
[Link]("Producer produced: " + i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}}}}
class Consumer extends Thread {
SharedData data;
Consumer(SharedData d) {
data = d;
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]("Consumer consumed: " + [Link]);
try {
[Link](500);
6
} catch (InterruptedException e) {
[Link](e);
}}}}
public class ProducerConsumerWithoutSync {
public static void main(String[] args) {
SharedData d = new SharedData();
Producer p = new Producer(d);
Consumer c = new Consumer(d);
[Link]();
[Link]();
}}
Note: Output (order may vary due to concurrent execution):
//With synchronization
package demothread;
class SharedData1 {
int value;
boolean hasValue = false; // flag to check availability
// Producer puts value
synchronized void produce(int v) {
while (hasValue) { // if value already produced, wait
7
try {
wait();
} catch (InterruptedException e) {
[Link](e);
}}
value = v;
hasValue = true;
[Link]("Producer produced: " + v);
notify(); // wake up consumer
}
// Consumer gets value
synchronized int consume() {
while (!hasValue) { // if no value yet, wait
try {
wait();
} catch (InterruptedException e) {
[Link](e);
}}
hasValue = false;
[Link]("Consumer consumed: " + value);
notify(); // wake up producer
return value;
}}
// Producer thread
class Producer1 extends Thread {
SharedData1 data;
Producer1(SharedData1 d) {
data = d;
8
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](i);
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}}}}
// Consumer thread
class Consumer1 extends Thread {
SharedData1 data;
Consumer1(SharedData1 d) {
data = d;
}
public void run() {
for (int i = 1; i <= 5; i++) {
[Link]();
try {
[Link](500);
} catch (InterruptedException e) {
[Link](e);
}}}}
// Main class
public class ProducerConsumerWithSync {
public static void main(String[] args) {
SharedData1 d = new SharedData1();
Producer1 p = new Producer1(d);
9
Consumer1 c = new Consumer1(d);
[Link]();
[Link]();
}}
10