im going to create two java threads(one thread to multiply numbers another thread to add numbers)

Viewed 187

I have to get the output like this:

Expected output

Here is my code. There are no errors. It starts with PlusThread and prints the first line. Then it will release the lock. After that MultiplyThread starts to run. It will print all the lines instead of notifying the PlusThread.

public class TestThread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Object lock = new Object();
        PlusThread pT = new PlusThread(lock, 2, 10);
        pT.start();
        MultiplyThread mT = new MultiplyThread(lock, 2, 10);
        mT.start();
    }
}

class PlusThread extends Thread {

    Object lock;
    int start, range;

    public PlusThread(Object lock, int start, int range) {
        super();
        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (this) {
            for (int i = start; i <= range; ++i) {

                System.out.println(i + " + " + i + " = " + (i + i));
                try {
                    this.wait();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

class MultiplyThread extends Thread {
    Object lock;
    int start, range;

    public MultiplyThread(Object lock, int start, int range) {

        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (this) {
            for (int j = start; j <= range; ++j) {
                System.out.println(j + " * " + j + " = " + (j * j));
                this.notify();
            }
        }
    }
}

Here is my output:

Actual output

2 Answers
try this:

public class TestThread {
    static Object lock = new Object();
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        PlusThread pT = new PlusThread(lock, 2, 10);
        pT.start();
        MultiplyThread mT = new MultiplyThread(lock, 2, 10);
        mT.start();
    }
}

class PlusThread extends Thread {

    Object lock;
    int start, range;

    public PlusThread(Object lock, int start, int range) {
        super();
        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (lock) {
            for (int i = start; i <= range; ++i) {

                System.out.println(i + " + " + i + " = " + (i + i));
                try {
                    lock.notify();
                    lock.wait();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }

}

class MultiplyThread extends Thread {
    Object lock;
    int start, range;

    public MultiplyThread(Object lock, int start, int range) {

        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (lock) {
            for (int j = start; j <= range; ++j) {

                System.out.println(j + " * " + j + " = " + (j * j));
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }

    }
}

This will work

static class PlusThread extends Thread {

    final Object lock;
    int start, range;

    public PlusThread(Object lock, int start, int range) {
        super();
        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (lock) {
            for (int i = start; i <= range; ++i) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(i + " + " + i + " = " + (i + i));
                lock.notify();
            }
        }
    }
}

static class MultiplyThread extends Thread {
    final Object lock;
    int start, range;

    public MultiplyThread(Object lock, int start, int range) {

        this.lock = lock;
        this.start = start;
        this.range = range;
    }

    @Override
    public void run() {
        synchronized (lock) {
            for (int j = start; j <= range; ++j) {
                System.out.println(j + " * " + j + " = " + (j * j));
                lock.notify();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
Related