微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在不使用 Java 库中的 CyclicBarrier 的情况下制作我自己的 CyclicBarrier

如何解决如何在不使用 Java 库中的 CyclicBarrier 的情况下制作我自己的 CyclicBarrier

我在大学的 PacMan 游戏中工作,基本上我必须制作自己的 CyclicBarrier,因为我无法使用 Java 的 CyclicBarrier 库。当鬼魂到达特定位置 (GhostGoal) 时,此屏障将用于保留鬼魂,并且它们必须等待更多鬼魂,直到我的 CyclicBarrier 的构造函数中给出的鬼魂数量达到最大值。 Ghosts 对象实现可运行(线程)。

我以这种方式构建了我的 CyclicBarrier:

public class Barreira {
    private int num_threads;
    private int count;
    
    public Barreira(int num_threads) {
        this.num_threads=num_threads;
    }
    
    public synchronized void await() {
        while(count < num_threads) {
            try {
                wait();
            } catch (InterruptedException e) {
                // Todo Auto-generated catch block
                e.printstacktrace();
            }
        }
        notifyAll();    
    }
    
    public synchronized void adiciona() {
        count+=1;
    }
    
    public int getNumThreadsNaBarreira() {
        return count;
    }
    
    public synchronized void reset() {
        count=0;
    }

}

调用屏障的代码(在用名称 barreira 初始化后)以这种方式在这个空白中:

public synchronized void arrivePosition(){
...

if((Object)floor[y][x] instanceof GhostGoal && entity instanceof Ghost ) {
            barreira.adiciona();
            barreira.await();
            
        }

}

然而,当一个鬼魂到达该位置时,所有的鬼魂都会停止移动,而不仅仅是在该位置的鬼魂......基本上一切都冻结了,因为在我的名为 barreira 的屏障中等待。

有人可以帮忙吗?

非常感谢。

我也在考虑这个解决方案(删除 await 方法并仅使用 adiciona 方法

public class Barreira {
    ...
    private List <Ghost> barreiraGhosts = new ArrayList<Ghost>();
    
public synchronized void adiciona(Ghost g) { //teste
    while(contador <num_threads) {
    try {
        contador+=1;
        barreiraGhosts.add(g);
        g.wait();
    } catch (InterruptedException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }
    }
    notifyAll();
    for(Ghost b: barreiraGhosts)
        b.run();
    reset();
}

}


public synchronized void arrivePosition(){
    ...
    
if((Object)floor[y][x] instanceof GhostGoal && entity instanceof Ghost ) {
            barreira.adiciona(entity);
        }

}

最好的解决方案是什么?

解决方法

嗯...)这是我在评论中所说的:

public class Test {

    public static void main(String[] args) throws Exception {
        final Thread[] threads = new Thread[5];

        final Barreira barreira = new Barreira(threads.length);

        for (int i = 0; i < threads.length; i++) {
            final Thread thread = new Thread(() -> {
                System.out.println("I'm started");
                barreira.adiciona();
                try {
                    barreira.await();
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                System.out.println("I'm finished");
            });
            threads[i] = thread;
        }

        for (final Thread thread : threads) {
            thread.start();
        }

        for (final Thread thread : threads) {
            thread.join();
        }
        System.out.println("All done");
    }

    public static class Barreira {
        private final int num_threads;
        private int count;

        public Barreira(int num_threads) {
            this.num_threads = num_threads;
        }

        public synchronized void await() throws InterruptedException { // it's a common pattern
            // to declare InterruptedException for possible long/time consuming operations
            while (count < num_threads) {
                wait();
            }
        }

        public synchronized void adiciona() {
            count += 1;
            notifyAll();
        }

        public synchronized int getNumThreadsNaBarreira() {
            return count;
        }

        public synchronized void reset() {
            count = 0;
        }
    }
}

就像一个魅力。我认为您在使用屏障时有问题,但不是屏障本身...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?