我将创建两个java线程(一个线程用于乘数,另一个线程用于加数)。

问题描述 投票:0回答:1

我必须得到这样的输出。

Expected output

这是我的代码 没有错误。它的开头是 PlusThread 并打印出第一行。然后它会释放锁。之后 MultiplyThread 开始运行。它将打印所有的行而不是通知 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();
            }
        }
    }
}

这是我的输出。

Actual output

java multithreading java-threads thread-synchronization
1个回答
2
投票
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();
                }

            }

        }

    }
}

0
投票

这就可以了

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();
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.