简体   繁体   English

在thread2进入method2之后,运行thread1以完成method1的信号

[英]Signal running thread1 to complete method1 after thread2 enters method2

I have 2 methods which need to function as below. 我有两种方法需要运行如下。

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
}

The easiest solution would be to synchronize both method1 and method2. 最简单的解决方案是同步method1和method2。 Another point of consideration is that method1 can be invoked by multiple threads (>10) and is common operation, while method2 is called very very rarely. 另一个需要考虑的问题是,method1可以由多个线程调用(> 10)并且是常见的操作,而method2很少被调用。 So, synchronizing method1 will seriously hamper multiprocessing capability. 因此,同步方法1将严重妨碍多处理能力。

I explored the possibility of using ReadWriteLock, with a readLock in method1 and writeLock in method2. 我探讨了使用ReadWriteLock的可能性,方法2中的readLock和method2中的writeLock。 But that doesn't help me with #1 in method2, ie. 但这对方法2中的#1没有帮助,即。 after a writeLock is acquired in method2, I want running threads in method1 to complete before I proceed method2. 在method2中获取writeLock之后,我希望在继续执行method2之前在method1中运行线程。

This is a classic usage of ReadWriteLock . 这是ReadWriteLock的经典用法。

See Java Concurrency with ReadWriteLock 请参阅使用ReadWriteLock的Java并发

The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. 只要没有写入器,读锁定可以由多个读取器线程同时保持。 The write lock is exclusive . 写锁是独占的

So when you request a write lock in #1 of method2 it will wait until a time when all read locks have been released by anyone calling method1 before taking the lock and returning. 所以,当你在#1请求写入锁定method2将等到的时候,所有的读锁已被释放被任何人调用method1取锁定并在返回之前。

Taking a write lock essentially performs both #1 and #2 of method2 . 以一个写锁基本上同时执行#1和#2 method2

暂无
暂无

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

相关问题 Thread2 等待 Thread1 完成启动 问题? Java - Thread2 waits Thread1 to complete to start Problem? Java 如何按线程1打印一半数字,按线程2打印第二一半 - How to print half number by thread1 and second half by thread2 如何从thread1等待直到thread2通知 - How to wait from thread1 until notified by thread2 为什么方法1和方法2在字节码级别相同? - Why is method1 and method2 the same at the Bytecode level? 在Java中,“ method1()。method2()。method3()”是什么意思? - what does “method1().method2().method3()” mean in java? 在另一个 java method1 中调用 java method2 并且不要等待 method2 响应并继续执行 - Call java method2 inside another java method1 and dont wait for method2 response and continue executing 我们是否需要创建一个字段'volatile',如果 Thread1 进入同步块,更新它,仍然在同步块内,线程 2 在同步之外读取字段? - Do we need to make a field 'volatile', if Thread1 enters sync block, updates it, is still inside the sync block, Thread2 outside of sync reads field? 对于良好的编码惯例,如果我们已经在方法1中进行了验证,那么我们是否仍需要在方法2中再次验证数据?方法1将数据传递给方法2? - For Good coding practice, do we still have to validate data again in method2 if we already validated in method1 & method1 passes that data to method2? 如果不能在 static 方法中使用“this”关键字,那么在给定代码中 method1() 如何调用 method2()? - If 'this' keyword can not be used inside a static method then how is method1() calling method2() in the given code? Java:我该如何减少if(method1()&& method2()…&& method25())? - java: how could i reduce if(method1()&&method2()…&&method25())?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM