【问题标题】:im going to create two java threads(one thread to multiply numbers another thread to add numbers)我要创建两个java线程(一个线程乘数另一个线程添加数字)
【发布时间】:2020-08-31 22:16:37
【问题描述】:

我必须得到这样的输出:

这是我的代码。没有错误。它以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();
            }
        }
    }
}

这是我的输出:

【问题讨论】:

  • 检查这个问题stackoverflow.com/questions/17472572/…它可能会有所帮助!
  • 不幸的是,Java 之神选择了synchronized 作为锁定锁的构造,因为锁 用于同步或协调不同线程的活动。锁仅用于一个目的:通过防止线程同时访问相同的变量来防止线程相互干扰。对于任何其他目的,您应该使用更高级别的同步对象;信号量、屏障、阻塞队列等

标签: java multithreading java-threads thread-synchronization


【解决方案1】:

这会起作用

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();
                }
            }
        }
    }
}

【讨论】:

    【解决方案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();
                    }
    
                }
    
            }
    
        }
    }
    

    【讨论】:

    • 谢谢您,先生!它可以工作..但它不会终止控制台
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多