简体   繁体   English

如何在Java中为两个不同的对象调用相同的方法

[英]How to call the same method for two different objects in Java

I am writing a program that is intended to have two different Miners collect resources of a map and bring it back to a city they belong to. 我正在编写一个程序,旨在让两个不同的矿工收集地图资源并将其带回他们所属的城市。 This is the method describing their collection: 这是描述其集合的方法:

public void harvest(ArrayList<Resource> rez, SimpleWindow w) {
    double min = Integer.MAX_VALUE;
    int tracker = 0;
    for (int i = 0; i < rez.size(); i++) {
        if (Math.hypot(rez.get(i).getX() - this.x, rez.get(i).getY() - this.y) < min) {
            min = Math.hypot(rez.get(i).getX() - this.x, rez.get(i).getY() - this.y);
            tracker = i;
        }
    }
    moveTo(rez.get(tracker).getX(), rez.get(tracker).getY(), w, rez);
    this.value = this.value + rez.get(tracker).getValue();
    SimpleWindow.delay(200);
    rez.get(tracker).undraw(w);
    rez.remove(tracker);
    moveTo(city.getX(), city.getY(), w, rez);
    city.addResource(this.value);
    SimpleWindow.delay(200);

}

When I call this method for two different miners in an infinite while-loop like this (where r is an ArrayList of resource-objects and w my graphical window)... 当我在像这样的无限while循环中为两个不同的矿工调用此方法时(其中r是资源对象的ArrayList,而w是我的图形窗口)...

Miner robot = new Miner(500, 235, city1);
Miner robot1 = new Miner(850, 430, city2);
while (true){
    robot.harvest(r, w);
    robot1.harvest(r, w);
    city1.draw(w);
}

...they simply take turns collecting resources instead of doing it at the same time. ...他们只是轮流收集资源,而不是同时进行。 I understand the reason for this but know of no way to solve the problem. 我了解原因,但无法解决问题。

I tried using separate threads like this... 我尝试使用像这样的单独线程...

Thread t1 = new Thread(new Runnable() {
    @Override
    public void run() {
        while (true){
            robot.harvest(r, w);
        }
    }
});

Thread t2 = new Thread(new Runnable() {
    @Override
    public void run() {
        while (true){
            robot1.harvest(r, w);
        }
    }
});

This worked to some degree as the miners mined at the same time, but it also produced a series of graphical glitches and problems with the ArrayList of the resource objects. 这在矿工同时进行开采的过程中起到了一定作用,但同时也产生了一系列图形故障和资源对象的ArrayList问题。 Is there a way to solve this problem? 有办法解决这个问题吗?

You should use a Collections.synchronizedList() insead of your ArrayList. 您应该在ArrayList中使用Collections.synchronizedList()

As for the graphical glitches, not really sure. 至于图形故障,不是很确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从java中的两个不同类调用相同的方法 - How to call same method from two different classes in java 如何使用 java 中的不同对象访问相同的方法 - How to access same method with different objects in java 如何在相同的接口方法上调用不同的远程对象并维护自己的数据? Java RMI - How to call different remote objects on same interface method and maintain their own data? java RMI Java编译器:具有相同名称和不同签名的两个方法如何匹配方法调用? - Java compiler: How can two methods with the same name and different signatures match a method call? 如何在多个不同的对象上调用相同的刻度方法? - How to call same tick-method on multiple different objects? 在方法调用期间如何使用同一类的两个对象? - How to use two objects of the same class during a method call? 如何在同一方法中执行两个不同的 ResultSet 对象 - How to execute two different ResultSet Objects in the same method 如何更新可用于 Java 中两个不同对象的方法 - How to update a method that can be used for two different objects in Java 如何从同一个java项目中的不同类调用另一个方法 - How to call another method from a different class in the same java project Java多态性-如何在不同的类中调用相同的方法 - Java polymorphism - how to call same method in different classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM