class Demo {
    volatile boolean part1done = false;

    synchronized void part1() {
        System.out.println("From thread1");
        part1done = true;
        System.out.println("Thread t1 surrendering");
        notify();
    }

    synchronized void part2() {
        System.out.println("Waiting for part1 to complete");
        while (!part1done) {
            try {
                wait();
            } catch (Exception e) {
                System.out.println(e.getClass());
            }
        }

        System.out.println("Part2 is done");
    }
}

public class Main {
    public static void main(String[] args) {
        Demo obj = new Demo();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                obj.part1();
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                obj.part2();
            }
        });

        t2.start();
        t1.start();
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: