NBKRIST Java Hub

Class Room Examples

← Back to Home
Thread Example
Multiple Threads Demo
Race Condition
Synchronized Method
Synchronized Block
Inter-Thread Communication
/** * Demonstrates how to create and run threads in Java using both * the Thread class and the Runnable interface. * * Author: Haribabu Mutchakala NBKRIST * Date: 15-10-2025 * Version: 1.0 */ class ThreadExample { /** * Thread created by extending the Thread class. */ static class MyThread extends Thread { private String message; public MyThread(String message) { this.message = message; } @Override public void run() { System.out.println("Thread class: " + message); } } /** * Thread created by implementing the Runnable interface. */ static class MyRunnable implements Runnable { private String message; public MyRunnable(String message) { this.message = message; } @Override public void run() { System.out.println("Runnable interface: " + message); } } public static void main(String[] args) { MyThread thread1 = new MyThread("Hello from MyThread!"); thread1.start(); MyRunnable runnableTask = new MyRunnable("Hello from MyRunnable!"); Thread thread2 = new Thread(runnableTask); thread2.start(); try { thread1.join(); thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("All threads have completed execution."); } }
/**
 * Demonstration of how multiple threads in Java can execute tasks concurrently
 * to reduce total execution time.
 *
 * Author: Haribabu Mutchakala NBKRIST
 * Date: 15-10-2025
 * Version: 1.0
 */
class Cook implements Runnable {

    private String name;
    private long timeToPrepareDish;
    private String dish;

    public Cook(String name, long timeToPrepareDish, String dish) {
        this.name = name;
        this.timeToPrepareDish = timeToPrepareDish;
        this.dish = dish;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " is cooking\t" + dish);
        try { Thread.sleep(timeToPrepareDish); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println(name + " has finished cooking.");
    }
}

public class MultipleThreadsDEmo {
    public static void main(String[] args) {
        Cook sambarCook = new Cook("Sambar Cook", 5000, "Sambar");
        Cook riceCook = new Cook("Rice Cook", 8000, "Rice");
        Cook beansFryCook = new Cook("Beans Fry Cook", 10000, "Chutney");
        Cook rasamCook = new Cook("Rasam Cook", 9000, "Rasam");

        Thread sambarThread = new Thread(sambarCook);
        Thread riceThread = new Thread(riceCook);
        Thread beansFryThread = new Thread(beansFryCook);
        Thread rasamThread = new Thread(rasamCook);

        sambarThread.start(); riceThread.start(); beansFryThread.start(); rasamThread.start();

        try {
            sambarThread.join(); riceThread.join(); beansFryThread.join(); rasamThread.join();
            System.out.println("Meal is ready to be served!");
        } catch (InterruptedException e) { e.printStackTrace(); }
    }
}
/**
 * Demonstrates the effect of race conditions in a multithreaded environment
 * when multiple threads access a shared resource without proper synchronization.
 *
 * Author: Haribabu Mutchakala NBKRIST
 * Date: 15-10-2025
 * Version: 1.0
 */
class Printer {
    public void print(String msg) {
        System.out.print("[" + msg);
        try { Thread.sleep(2000); } catch (InterruptedException e) {}
        System.out.println("]");
    }
}

class PrintThread implements Runnable {
    private Printer printer; private String msg;
    public PrintThread(Printer printer, String msg) { this.printer = printer; this.msg = msg; }
    @Override
    public void run() { printer.print(msg); }
}

public class RaceCondition {
    public static void main(String[] args) throws InterruptedException {
        Printer printer = new Printer();
        Thread t1 = new Thread(new PrintThread(printer, "Hello"));
        Thread t2 = new Thread(new PrintThread(printer, "Welcome"));
        Thread t3 = new Thread(new PrintThread(printer, "To Java"));
        t1.start(); t2.start(); t3.start();
        t1.join(); t2.join(); t3.join();
        System.out.println("All threads have finished execution of Printing.");
    }
}
/**
 * Demonstrates proper synchronization of multiple threads in Java
 * using the synchronized method.
 *
 * Author: Haribabu Mutchakala NBKRIST
 * Date: 15-10-2025
 * Version: 1.0
 */
class Printer {
    public synchronized  void print(String msg) {
        System.out.print("[" + msg);
        try { Thread.sleep(2000); } catch (InterruptedException e) {}
        System.out.println("]");
    }
}

class PrintThread implements Runnable {
    private Printer printer; private String msg;
    public PrintThread(Printer printer, String msg) { this.printer = printer; this.msg = msg; }
    @Override
    public void run() { printer.print(msg); }
}

public class RaceCondition {
    public static void main(String[] args) throws InterruptedException {
        Printer printer = new Printer();
        Thread t1 = new Thread(new PrintThread(printer, "Hello"));
        Thread t2 = new Thread(new PrintThread(printer, "Welcome"));
        Thread t3 = new Thread(new PrintThread(printer, "To Java"));
        t1.start(); t2.start(); t3.start();
        t1.join(); t2.join(); t3.join();
        System.out.println("All threads have finished execution of Printing.");
    }
}
purpose: Inter-thread communication using wait, notify, notifyAll, how threads communicate gracefully?
/**
 * Demonstrates proper synchronization of multiple threads in Java
 * using a synchronized block.
 *
 * Author: Haribabu Mutchakala NBKRIST
 * Date: 15-10-2025
 * Version: 1.0
 */
class Printer {
    public void print(String msg) {
        synchronized(this) {
            System.out.print("[" + msg);
            try { Thread.sleep(2000); } catch (InterruptedException e) {}
            System.out.println("]");
        }
    }
}

class PrintThread implements Runnable {
    private Printer printer; private String msg;
    public PrintThread(Printer printer, String msg) { this.printer = printer; this.msg = msg; }
    @Override
    public void run() { printer.print(msg); }
}

public class RaceCondition {
    public static void main(String[] args) throws InterruptedException {
        Printer printer = new Printer();
        Thread t1 = new Thread(new PrintThread(printer, "Hello"));
        Thread t2 = new Thread(new PrintThread(printer, "Welcome"));
        Thread t3 = new Thread(new PrintThread(printer, "To Java"));
        t1.start(); t2.start(); t3.start();
        t1.join(); t2.join(); t3.join();
        System.out.println("All threads have finished execution of Printing.");
    }
}