“同步”是什么意思?到底是怎么做的?锁定一个函数还是锁定一个对象函数?

发布于 2024-10-10 14:07:06 字数 432 浏览 2 评论 0原文

我想知道“同步”在java中到底是如何工作的。

假设我模拟了一个由多个字段组成的棋盘游戏。我将字段实现为类(Field),将板实现为包含多个字段的类(Board)。 进一步说,我在 Field 中建模了 moveTo(Player pl) 方法,以便玩家移动到该字段。每个玩家都由一个线程代表。

尽管所有线程都应该同时执行某些操作(例如掷骰子),但一次应该只有一个玩家移动。

我如何确保这一点? 使 moveTo(Player pl) 方法同步就足够了吗?或者我需要在 Board 中使用级联方法来确保一次只有一名玩家移动? (或者有更好的解决方案)?

要将其归结为底线:
“同步”会锁定每个具有此方法的对象中的方法,还是仅同步锁定当前正在使用的对象中的方法?
如果是第二种情况:是否有一种简单的方法可以为每个实现了该方法的对象锁定该方法?

谢谢你!!!

I am wondering how exactly "synchronized" works in java.

Let's say I model a board-game that consists of a number of fields. I implement the fields as a class (Field) and the board as a class (Board) that contains a number of fields.
Let's further say I modelled a method moveTo(Player pl) in Field, for a player to move to that field. Each player is represented by a thread.

Although all the threads should do some actions simultaneously (for example rolling their dices), there should only be one player that moves at a time.

How would I ensure that?
Is it enough to make the method moveTo(Player pl) synchronized? Or would I need a cascading method in Board to make sure that only one player moves at a time? (Or is there a better solution)?

To bring it to the bottom line:
Would "synchronized" lock a method in EVERY object that has this method or would synchronized lock a method only in the object that is currently in use?
And if the second is the case: is there an easy way to lock a method for every object that has this method implemented?

Thank you!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ら栖息 2024-10-17 14:07:06

我认为您想要的是以下内容:

class Field {

    // instance of the board
    private Board board; 

    public void moveTo(Player p){ 
        synchronized (board) {
            // move code goes here
        }
    }
}

这将使每个棋盘一次只有一名玩家在移动。如果在单板上获得同步锁,则一次只有一个Field可以进入同步锁。 (假设有一块板)

如果您只是简单地写:

public synchronized void moveTo(Player p){ 

您只能确保玩家不能一次移动到同一个区域。这是因为当一个方法在定义中使用synchronized 编写时,它将在对象级别锁定。因此,Field 的每个实例都将是它自己的锁定对象,因此,只要玩家不移动到同一个 Field,他们仍然可以同时移动。

What I think you would want is the following:

class Field {

    // instance of the board
    private Board board; 

    public void moveTo(Player p){ 
        synchronized (board) {
            // move code goes here
        }
    }
}

This will make it so that per board, only one player is moving at a time. If you get the synchronization lock on the board, only one Field may enter the synchronized lock at a time. (Assuming one board)

If you simply wrote:

public synchronized void moveTo(Player p){ 

you would only be assuring that players couldn't move to the same Field at a time. This is because when a method is written with synchronized in the definition, it will lock at the object level. So, each instance of Field will be it's own lock object, and thus, players could still move at the same time as long as they weren't moving to the same Field.

望她远 2024-10-17 14:07:06

Synchronized 锁定对象,而不是方法。 Java没有函数。

如果您希望某个方法在所有对象中仅调用一次,您可以锁定共享对象,例如类。

我建议您只需要一个线程来执行所有移动/操作,这将显着简化代码。为什么一定要多线程呢?

synchronized locks an object, not a method. Java doesn't have functions.

If you want a method to be called only once across all object you can lock on a shared object, such as the class.

I would suggest you only need one thread to perform all the moves/operations and this would simplify the code significantly. Why does it have to be multi-threaded?

海的爱人是光 2024-10-17 14:07:06

同步锁定对资源的访问。该资源可以是函数或对象(包括 this 对象)。

如果没有具体的例子,我们无法给您具体的建议。

在你的例子中,如果玩家一次只能玩一回合,为什么他们需要在自己的线程中?为什么他们需要同时掷骰子?

同步函数会锁定对该方法的该实例版本的调用(除非它是静态方法)。

如果是第二种情况:是否有一种简单的方法可以为每个实现了该函数的对象锁定该函数?

这似乎表明可以重新评估您的设计。您的对象的实例方法是否真的共享状态,以便它们需要彼此同步?为什么?

Synchronisation locks access to a resource. That resource can either be a function or an object (Including the this object).

We can't give you specific advice without a specific example.

In your example, if the players can only take 1 turn at a time, why do they need to be in their own threads? Why do they need to roll their dice at the same time?

synchronising a function locks calls to that instance's version of that method (unless it's a static method).

And if the second is the case: is there an easy way to lock a function for every object that has this function implemented?

This seems to suggest that your design could be re-evaluated. Do your object's instance methods really share state such that they need their methods synchronised with each other? Why?

葬花如无物 2024-10-17 14:07:06

这取决于它的使用地点。

如果在典型方法中使用,它会专门获取相关对象的“锁”对象。如果其他线程需要独占访问同一对象的“锁”对象(非同步方法不需要独占访问锁),则没有其他线程可以处理该对象中的方法。

如果在静态方法中使用,它会专门获取代表该类的对象的锁对象。如果其他线程需要独占访问同一类对象的“锁”对象(非同步方法不需要独占访问锁),则其他线程都无法处理该类对象中的方法。

如果与显式声明的对象一起使用,它会独占地获取显式声明的对象的锁定对象。同样,在此期间没有其他线程可以独占地获取该对象的锁。

一旦离开同步块的范围,您就放弃了对锁的独占保留。

It depends on where it is used.

If used in a typical method, it exclusively grabs the "lock" object of the Object in question. No other Thread can process methods in that object if they need exclusive access to the same Object's "lock" object (non-synchronized methods don't require exclusive access to the lock).

If used in a static method, it exclusively grabs the lock object of the Object representing the Class. No other Thread can process methods in that Class object if they need exclusive access to the same Class Object's "lock" object (non-synchronized methods don't require exclusive access to the lock).

If used with an explicitly stated object, it exclusively grabs the lock object of the explicitly stated object. Again, no other Thread can exclusively grab the lock of that object during that time.

Once you leave the scope of a synchronized block, you relinquish your exclusive hold on the lock.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文