(OOPL9) Multithreading
🧵

(OOPL9) Multithreading

 

Multiplication Tables

class MultiplicationTable extends Thread { private int number; public MultiplicationTable(int number) { this.number = number; } public void run() { System.out.println("Multiplication table for " + number + ":"); for (int i = 1; i <= 10; i++) { System.out.println(number + " x " + i + " = " + (number * i)); } } } public class Tables { public static void main(String[] args) { MultiplicationTable table5 = new MultiplicationTable(5); MultiplicationTable table7 = new MultiplicationTable(7); table5.start(); try { table5.join(); // Wait for table5 to finish before starting table7 } catch (InterruptedException e) { e.printStackTrace(); } table7.start(); } }
PS F:\oopsies> Multiplication table for 5: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50 Multiplication table for 7: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70

Matrix Sum Calculator

class RowSum implements Runnable { private int[] row; private int partialSum; public RowSum(int[] row) { this.row = row; this.partialSum = 0; } public int getPartialSum() { return partialSum; } public void run() { for (int value : row) { partialSum += value; } } } public class MatrixSum { public static void main(String[] args) { int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; int numRows = matrix.length; int totalSum = 0; RowSum[] calculators = new RowSum[numRows]; Thread[] threads = new Thread[numRows]; for (int i = 0; i < numRows; i++) { calculators[i] = new RowSum(matrix[i]); threads[i] = new Thread(calculators[i]); threads[i].start(); } try { for (int i = 0; i < numRows; i++) { threads[i].join(); totalSum += calculators[i].getPartialSum(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Total Sum: " + totalSum); } }
PS F:\oopsies> Total Sum: 45

Producer-Consumer Problem

import java.util.ArrayList; class Buffer { private ArrayList<Integer> list = new ArrayList<>(); private int capacity; public Buffer(int capacity) { this.capacity = capacity; } public synchronized void produce(int item) { while (list.size() == capacity) { try { wait(); // Wait if the buffer is full } catch (InterruptedException e) { e.printStackTrace(); } } list.add(item); System.out.println("Produced: " + item); notify(); // Notify waiting consumer } public synchronized int consume() { while (list.isEmpty()) { try { wait(); // Wait if the buffer is empty } catch (InterruptedException e) { e.printStackTrace(); } } int item = list.remove(0); System.out.println("Consumed: " + item); notify(); // Notify waiting producer return item; } } class Producer implements Runnable { private Buffer buffer; public Producer(Buffer buffer) { this.buffer = buffer; } @Override public void run() { for (int i = 1; i <= 5; i++) { buffer.produce(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { private Buffer buffer; public Consumer(Buffer buffer) { this.buffer = buffer; } @Override public void run() { for (int i = 0; i < 5; i++) { buffer.consume(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ProducerConsumer { public static void main(String[] args) { Buffer buffer = new Buffer(5); Thread producerThread = new Thread(new Producer(buffer)); Thread consumerThread = new Thread(new Consumer(buffer)); producerThread.start(); consumerThread.start(); } }
PS F:\oopsies> Produced: 1 Consumed: 1 Produced: 2 Consumed: 2 Produced: 3 Consumed: 3 Produced: 4 Consumed: 4 Produced: 5 Consumed: 5