diff --git a/exercise/EvenOddPrinter.java b/exercise/EvenOddPrinter.java new file mode 100644 index 0000000..0cd7b5d --- /dev/null +++ b/exercise/EvenOddPrinter.java @@ -0,0 +1,45 @@ +public class EvenOddPrinter { + private static volatile boolean flag = true; // Use volatile to ensure visibility across threads + + public static void main(String[] args) { + Runnable odd = () -> { + for (int i = 1; i <= 10; i += 2) { + synchronized (EvenOddPrinter.class) { + while (!flag) { + try { + EvenOddPrinter.class.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread().getName() + " " + i); + flag = false; + EvenOddPrinter.class.notify(); // Notify waiting thread (even thread) + } + } + }; + + Runnable even = () -> { + for (int i = 2; i <= 10; i += 2) { + synchronized (EvenOddPrinter.class) { + while (flag) { + try { + EvenOddPrinter.class.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread().getName() + " " + i); + flag = true; + EvenOddPrinter.class.notify(); // Notify waiting thread (odd thread) + } + } + }; + + Thread t1 = new Thread(odd, "OddThread"); + Thread t2 = new Thread(even, "EvenThread"); + + t1.start(); + t2.start(); + } +} diff --git a/exercise/PrintSequenceRunnable.java b/exercise/PrintSequenceRunnable.java new file mode 100644 index 0000000..00f8dd2 --- /dev/null +++ b/exercise/PrintSequenceRunnable.java @@ -0,0 +1,45 @@ +public class PrintSequenceRunnable implements Runnable { + public static final int PRINT_NUMBERS_UPTO = 10; + static int number = 1; + int remainder; + static Object lock = new Object(); + + public PrintSequenceRunnable(int remainder) { + this.remainder = remainder; + } + + @Override + public void run() { + while (number < PRINT_NUMBERS_UPTO - 1) { + synchronized (lock) { + while (number % 3 != remainder) { + // Wait for numbers other than the remainder + try { + lock.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println(Thread.currentThread().getName() + " " + number); + number++; + lock.notifyAll(); + } + } + } +} + +public class PrintThreadsSequentiallyMain { + public static void main(String[] args) { + PrintSequenceRunnable runnable1 = new PrintSequenceRunnable(1); + PrintSequenceRunnable runnable2 = new PrintSequenceRunnable(2); + PrintSequenceRunnable runnable3 = new PrintSequenceRunnable(0); + + Thread t1 = new Thread(runnable1, "T1"); + Thread t2 = new Thread(runnable2, "T2"); + Thread t3 = new Thread(runnable3, "T3"); + + t1.start(); + t2.start(); + t3.start(); + } +} diff --git a/exercise/ProducerConsumerExample.java b/exercise/ProducerConsumerExample.java new file mode 100644 index 0000000..c4ef488 --- /dev/null +++ b/exercise/ProducerConsumerExample.java @@ -0,0 +1,66 @@ +import java.util.LinkedList; +import java.util.Queue; +import java.util.Random; + +public class ProducerConsumerExample { + private static final int BUFFER_CAPACITY = 5; + private static final Queue buffer = new LinkedList<>(); + private static final Random random = new Random(); + + public static void main(String[] args) { + Thread producerThread = new Thread(new Producer(), "ProducerThread"); + Thread consumerThread = new Thread(new Consumer(), "ConsumerThread"); + + producerThread.start(); + consumerThread.start(); + } + + static class Producer implements Runnable { + @Override + public void run() { + while (true) { + synchronized (buffer) { + try { + while (buffer.size() == BUFFER_CAPACITY) { + System.out.println("Buffer is full. Producer is waiting..."); + buffer.wait(); + } + + int newItem = random.nextInt(100) + 1; + System.out.println("Produced: " + newItem); + buffer.offer(newItem); + + buffer.notify(); // Notify consumer thread that item is available + Thread.sleep(1000); // Simulate some delay + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + } + } + + static class Consumer implements Runnable { + @Override + public void run() { + while (true) { + synchronized (buffer) { + try { + while (buffer.isEmpty()) { + System.out.println("Buffer is empty. Consumer is waiting..."); + buffer.wait(); + } + + int item = buffer.poll(); + System.out.println("Consumed: " + item); + + buffer.notify(); // Notify producer thread that space is available + Thread.sleep(1500); // Simulate some delay + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + } + } +}