首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用多线程技术实现机器人的运动序列

利用多线程技术实现机器人的运动序列
EN

Stack Overflow用户
提问于 2021-03-30 04:29:49
回答 1查看 39关注 0票数 0

我有一段代码,我想使用wait()和notify()来确保机器人腿的移动顺序。代码如下:

代码语言:javascript
复制
public class Leg implements Runnable {

    private final Object monitor = new Object();
    private final String name;

    public Leg(String name) {
        this.name = name;
    }
    @Override
    public void run() {
        while (true) {
            synchronized (monitor) {
                move();
                monitor.notify();
                try {
                    monitor.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

    }
    private void move() {
        System.out.println(name);
    }


    public static void main(String[] args) {
        CompletableFuture.allOf(
                CompletableFuture.runAsync(new Leg("left")),
                CompletableFuture.runAsync(new Leg("right"))
        ).join();
    }
}

现在的输出如下:

代码语言:javascript
复制
left 
right
// and then it stops moving.

我希望代码继续移动(不只移动一次)。这就是为什么我感兴趣的原因是我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-03-30 17:31:01

你有两个Leg对象和两个线程要处理,每个线程都有一个monitor对象,所以当你使用notifywait时,它只影响一个线程(当前正在运行的线程):

代码语言:javascript
复制
synchronized (monitor) {
    move();
    monitor.notify(); // notify current thread, it's no meaning
    try {
        monitor.wait(); // current thread will block here and there is no thread wake up it
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

也许这就是你需要的:

代码语言:javascript
复制
import java.util.concurrent.CompletableFuture;

public class Leg implements Runnable {

    private final String name;
    private Leg nextLeg;
    private volatile boolean active;

    public Leg(String name) {
        this.name = name;
    }

    public void setNextLeg(Leg nextLeg) {
        this.nextLeg = nextLeg;
    }

    public void active() {
        synchronized (this) {
            this.active = true;
            notify();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                synchronized (this) {
                    while (!active) {
                        wait();
                    }
                    move();
                    active = false;
                    nextLeg.active();
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    private void move() {
        System.out.println(name);
    }


    public static void main(String[] args) {
        Leg left = new Leg("left");
        Leg right = new Leg("right");
        left.setNextLeg(right);
        right.setNextLeg(left);
        left.active();
        CompletableFuture.allOf(
                CompletableFuture.runAsync(left),
                CompletableFuture.runAsync(right)
        ).join();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66861236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档