我有 2 种方法需要按以下方式运行。
method1 {
1. run logic and insert into table1
}
method2 {
1. wait till other threads finish method1
2. block future threads from starting method1
3. update table1
}
最简单的解决方案是同步方法 1 和方法 2。还有一点需要考虑的是,method1可以被多个线程(>10)调用,并且是普通操作,而method2很少被调用。因此,同步方法 1 将严重影响多处理能力。
我探索了使用 ReadWriteLock 的可能性,在方法 1 中使用 readLock,在方法 2 中使用 writeLock。但这对方法2中的#1没有帮助,即。在方法 2 中获取 writeLock 后,我希望在方法 1 中运行线程完成,然后再继续方法 2。