简体   繁体   English

调用 RMI 方法会释放同步锁吗?

[英]Does calling an RMI method free the synchronized lock?

I call a RMI method and it's inside a synchronized block.我调用了一个 RMI 方法,它位于一个同步块中。 Something like this, where the method appendEntries is the RMI method being called:像这样,其中方法appendEntries是被调用的 RMI 方法:

public void run() {
    synchronized(matchIndex.get(followerId)) {
        ArrayList<Entry> reqs = new ArrayList<>();
        for (int i = matchIndex.get(followerId) + 1; i <= log.lastIndex(); i++) {
            reqs.add(log.get(i));
        }
   
        try {
            answer = server.serverList.get(followerId).appendEntries(reqs);
            if (answer.isSuccessful()) {
                matchIndex.replace(followerId, matchIndex.get(followerId) + reqs.size());
            } else {
                System.out.println("Not Successfull.");
            }
        } catch (Exception e) {
            e.printStackTrace();    
        }
    }
}    

While this thread is waiting for the RMI method's answer, does it free the lock that's in matchIndex.get(followerId) ?当这个线程正在等待 RMI 方法的答案时,它是否释放了matchIndex.get(followerId)的锁?

For better context, I ask this because, apparently, with various threads being used and no unsuccessful answers, there comes a random point where two threads are calling appendEntries for the same followerId and then a common Entry in reqs , which shouldn't happen and I suspect that's the cause.为了更好的背景下,我问这个是因为,很显然,与正在使用的各种线程,没有不成功的答案,总会有两个线程都呼吁同appendEntries随机点followerId ,然后在一个共同的输入reqs ,这不应该发生,我怀疑这就是原因。

If you need further explanation, I'm implementing the consensus algorithm Raft for a class project.如果你需要进一步的解释,我正在为一个班级项目实现共识算法 Raft。 If you're not familiar with it, basically this simplified piece of code is part of the "Leader" logic and he has to send the entries that are in its log to his "Followers".如果您不熟悉它,基本上这段简化的代码是“领导者”逻辑的一部分,他必须将其日志中的条目发送给他的“追随者”。 The logs on the leader and on the followers have to be eventually consistent.领导者和追随者上的日志必须最终一致。 This piece of code is on a thread that is called when the "Leader" wants to send its log entries to a specific follower, but he shouldn't call the appendEntries method more than once at a time for each follower.这段代码位于一个线程上,当“领导者”想要将其日志条目发送给特定的追随者时会调用该appendEntries ,但他不应为每个追随者一次多次调用appendEntries方法。 The matchIndex are the positions of the last entry that the leader knows of for each follower. matchIndex是领导者知道的每个跟随者的最后一个条目的位置。

I hope it's not too much or too little information, it's my first Stack Overflow question after years of using this website and any help would be appreciated.我希望它不是太多或太少的信息,这是我使用本网站多年后的第一个 Stack Overflow 问题,任何帮助将不胜感激。

My problem, as @NathanHughes pointed out, was that I was using the replace method on the object that had the lock and it messed up the synchronized.正如@NathanHughes 所指出的,我的问题是我在具有锁的对象上使用了 replace 方法,它搞砸了同步。 I created a new array of objects specifically for the purposes of locking and it now works flawlessly.我创建了一个专门用于锁定目的的新对象数组,现在它可以完美地工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM